VUE3项目学习系列--项目基础配置(四)

一、环境变量配置

项目开发过程中会经历开发环境、测试环境、生产环境三种状态,对与环境变量的配置需求不同,因此需要在项目中进行环境变量的配置。

1.在项目根目录下添加如下3个文件

javascript 复制代码
.env.development

.env.production

.env.test

文件中输入对应的配置信息

javascript 复制代码
# 变量必须以VITE_为前缀才能暴露给外部读取
NODE_ENV = 'development'
VITE_APP_TITLE = 'vue-admin'
VITE_APP_BASE_API = '/api'
VITE_SERVE = 'http://sph-api.atguigu.cn'

在package.json中配置运行命令

javascript 复制代码
 "scripts": {
    "build:test": "vue-tsc && vite build --mode test",
    "build:pro": "vue-tsc && vite build --mode production"
}

使用过程通过:import.meta.env 获取配置信息

在main.ts中引用测试,当前是开发环境因此只能打印开发环境信息:console.log(import.meta.env);

二、SVG图标配置

1、安装依赖插件

javascript 复制代码
pnpm install vite-plugin-svg-icons -D

2、在vite.config.ts中配置插件,对应的图标文件从src/assets/icons下获取,主要路径

javascript 复制代码
// 引入svg
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]',
    }),
   ]
})

3、main.ts中配置插件

javascript 复制代码
// svg插件配置代码
import 'virtual:svg-icons-register'

4、图标使用

(1)初级用法,直接在页面中引用

javascript 复制代码
<template>
  <div>
    <h2>SVG图标测试</h2>
    <svg style="width: 50px;height:50px">
      <!-- fill颜色生效需要在图标中删除已有的fill属性值 -->
      <use xlink:href="#icon-edit" fill="red"></use>
    </svg>
  </div>
</template>

(2)封装用法

在components下创建svgIcon文件夹及index.vue文件,将svn图标属性参数化,便于在父组件中使用时直接传递相关参数即可:属性需要在属性明前加分号才会生效

javascript 复制代码
<template>
    <svg :style="{width,height}">
        <!-- fill颜色生效需要在图标中删除已有的fill属性值 -->
        <use :xlink:href="prefix + name" :fill="color"></use>
    </svg>
</template>

<script setup lang="ts">
    // 接受父组件传递过来的参数
    defineProps({
        // xlink:href图标前缀
        prefix: {
            type: String,
            default: '#icon-'
        },
        // 受父组件传递的图标名称
        name: String,
        // 接受父组件传递的颜色
        color: {
            type: String,
            default: ''
        },
        // 图标宽度
        width: {
            type: String,
            default: "30px"
        },
        // 图标高度
        height: {
            type: String,
            default: "30px"
        },
    })
</script>

<style scoped>

</style>

页面中使用时引入组件:

javascript 复制代码
  // 引入svg封装的组件
  import svgIcon from '@/components/SvgIcon/index.vue'

  <svg-icon name="edit" color="pink" width="50px" height="50px"></svg-icon>
  <svgIcon name="copy" color="" width="50px" height="50px"></svgIcon>

三、注册组件为全局组件

由于对组件的使用需要在每个页面都引入会增加很不多不便,因此将组件注册为全局组件,一次引入,可以在整个项目中使用。

1、在components下创建index.ts文件

javascript 复制代码
// 对外暴露插件对象
import SvgIcon from "./SvgIcon/index.vue"
import Pagintation from "./Pageination/index.vue" 

// 全局变量
const allGloablComponent = {SvgIcon,Pagintation}
export default{
    // 此处必须为install方法名
    install(app){
        // 注册项目全部的全局组件
        Object.keys(allGloablComponent).forEach(key =>{
            // 注册为全局变量
         app.component(key,allGloablComponent[key]);
        })
    }
}

2、在main.ts中注入全局变量

javascript 复制代码
// 1、引入自定义插件对象:注册项目的全局组件
import gloalComponent from '@/components'
app.use(gloalComponent)
相关推荐
比特森林探险记1 天前
React API集成与路由
前端·react.js·前端框架
爱上妖精的尾巴1 天前
8-1 WPS JS宏 String.raw等关于字符串的3种引用方式
前端·javascript·vue.js·wps·js宏·jsa
hvang19881 天前
某花顺隐藏了重仓涨幅,通过chrome插件计算基金的重仓涨幅
前端·javascript·chrome
Async Cipher1 天前
TypeScript 的用法
前端·typescript
web打印社区1 天前
vue页面打印:printjs实现与进阶方案推荐
前端·javascript·vue.js·electron·html
We་ct1 天前
LeetCode 30. 串联所有单词的子串:从暴力到高效,滑动窗口优化详解
前端·算法·leetcode·typescript
木卫二号Coding1 天前
Docker-构建自己的Web-Linux系统-Ubuntu:22.04
linux·前端·docker
CHU7290351 天前
一番赏盲盒抽卡机小程序:解锁惊喜体验与社交乐趣的多元功能设计
前端·小程序·php
RFCEO1 天前
前端编程 课程十二、:CSS 基础应用 Flex 布局
前端·css·flex 布局·css3原生自带的布局模块·flexible box·弹性盒布局·垂直居中困难
天若有情6731 天前
XiangJsonCraft v1.2.0重大更新解读:本地配置优先+全量容错,JSON解耦开发体验再升级
前端·javascript·npm·json·xiangjsoncraft