vue3 搭建项目模板 之 初始化项目
创建vue项目 ( js 版本)
npm create vite
npm i
prettier的安装配置使用
"devDependencies": {
"@vitejs/plugin-vue": "^2.3.3",
"vite": "^2.9.9",
"prettier":"^2.2.1"
}
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 120,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"semi": false,
"htmlWhitespaceSensitivity": "ignore"
}
vite.config.js 配置 ( 相关环境设置)
- 本地打包配置
- 打包后 部署到本地服务器 查看打包后的效果
import vue from '@vitejs/plugin-vue'
export default ({
command,
mode
}) => {
return {
base: "./",
server: {
host: '0.0.0.0',
port: 5006,
open: false,
},
preview: {
port: 5006,
host: '0.0.0.0',
strictPort: true
},
plugins: [
vue({})
]
}
}
vite多环境配置
为了防止意外地将一些环境变量泄漏到客户端,只有以 VITE_ 为前缀的变量才会暴露给经过 vite 处理的代码
# 定义环境必须以 VITE_APP_ 开头 开发环境
VITE_APP_ENV = 'dev'
# VITE_APP_BASE_URL = 'https://github.jzfai.top/micro-service-api'
# 需要在package.json文件配置脚步指令 "dev": "vite --mode serve-dev --host",
VITE_APP_BASE_URL = 'dev'
#The defined variable must start with VITE_APP_
VITE_APP_ENV = 'test'
# VITE_APP_BASE_URL = 'http://localhost:20156/api'
VITE_APP_BASE_URL = 'test'
VITE_APP_ENV = 'test'
#自动获取地址推荐
#VITE_APP_BASE_URL = '/micro-service-api'
VITE_APP_BASE_URL = 'https://github.jzfai.top/micro-service-api'
VITE_APP_ENV = 'prod'
# The defined variable must start with VITE_APP_
VITE_APP_BASE_URL = 'https://github.jzfai.top/micro-service-api'
"scripts": {
"dev": "vite --mode serve-dev --host",
"test": "vite --mode serve-test --host",
"build:test": "vite build --mode build-test",
"build": "vite build --mode build",
"preview": "npm run build && vite preview "
},
自动导入插件 ( 基础 )
"devDependencies": {
"unplugin-auto-import":"~0.5.11"
}
- 安装依赖
npm i - 配置
- AutoImport 相关里面都需要手输入,才会自动生成
anto-import.d.ts文件
import vue from '@vitejs/plugin-vue'
import AutoImport from "unplugin-auto-import/vite"
export default ({
command,
mode
}) => {
return {
plugins: [
vue({}),
AutoImport({
imports:['vue'],
dts:'src/auto-import.d.ts'
})
]
}
}
import { ref } from 'vue'
自动引入插件 使用hooks
- hooks / global / useCommon.js ( 定义hooks )
import {
onMounted
} from "vue"
export const useCommonExample = () => {
onMounted(() => {
console.log("导出了useCommonExample");
})
}
export const useCommon = useCommonExample
export default useCommonExample()
- vite.config.js 配置hooks 的自动挂载
import vue from '@vitejs/plugin-vue'
import path, {
resolve
} from 'path'
import AutoImport from "unplugin-auto-import/vite"
export default ({
command,
mode
}) => {
return {
base: "./",
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
}
},
plugins: [
vue({}),
AutoImport({
imports: ['vue', {
'@/hooks/global/useCommon': ['useCommon']
}],
dts: 'src/auto-import.d.ts'
})
]
}
}
<template>
<div></div>
</template>
<script setup>
useCommon()
</script>
<style>
</style>
自动引入插件 生成 eslintrc-auto-import.json文件 ( eslint相关的东西)
plugins: [
vue({}),
AutoImport({
imports: ['vue', {
'@/hooks/global/useCommon': ['useCommon']
}],
eslintrc: {
enabled: true,
filepath: './.eslintrc-auto-import.json',
globalsPropValue: true
},
dts: 'src/auto-import.d.ts'
})
]
|