1.使用別名需要用到node的path,vite.config.ts 提示找不到對應(yīng)模塊
npm install @types/node --save-dev
2.報錯unable to verify the first certificate at TLSSocket.onConnectSecure
采用接口https必須配置 HTTPS& 自簽名證書
- 在自己選一個文件的地方創(chuàng)建新文件夾,生成ca證書
mkcert create-ca再生成cert證書mkcert create-cert - 將剛剛生成的cert.crt和cert.key兩個拷貝到項目的src/ssl文件夾中,沒有可以新建一個ssl目錄
- vue3在vite.config.js中寫入以下關(guān)鍵代碼
server: {
https: {// 是否開啟 https
// 主要是下面兩行的配置文件,不要忘記引入 fs 和 path 兩個對象
cert: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.crt')),
key: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.key'))
},
// secure必須設(shè)置,不然一直報錯unable to verify the first certificate at TLSSocket.onConnectSecure
proxy: {
'/api': {
target: baseUrl,
changeOrigin: true,
secure: false,//如果您想驗證SSL證書
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
https://blog.csdn.net/xfjpeter/article/details/121480873

備用圖片

1669096650857.png

1669096682369.png
3.獲取環(huán)境env
const ENV = loadEnv(mode, process.cwd())
4.動態(tài)路由
import.meta.glob 函數(shù)從文件系統(tǒng)導(dǎo)入多個模塊
const modules = import.meta.glob('../views/**/**.vue')
const routerPackag = (routers:any) => { // 動態(tài)添加可訪問路由表
if (routers) {
routers.filter((itemRouter:any) => {
if (itemRouter.component) {
if (itemRouter.component === 'layout') { // Layout組件特殊處理
router.addRoute({ name: 'layout', path: '', component: modules['../views/layout/index.vue'] })
} else {
router.addRoute('layout', { // 是父組件 add-route添加進(jìn)父組件chilren里
// path: '/:code' + itemRouter.path,
path: itemRouter.path,
name: itemRouter.component_name,
meta: { ...itemRouter.meta },
component: modules[`../views/${itemRouter.component}.vue`]
})
}
}
if (itemRouter.children && itemRouter.children.length) {
routerPackag(itemRouter.children)
}
return true
})
router.addRoute('NotFound', {
path: '/:pathMatch(.*)*',
redirect: '/404'
})
}
}