vite+vue3+ts報錯

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'
    })
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容