亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決vue3+vite配置unplugin-vue-component找不到Vant組件

 更新時間:2023年09月20日 11:44:11   作者:天問  
這篇文章主要為大家介紹了vue3+vite配置unplugin-vue-component找不到Vant組件問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

使用 vue3 + vite + Vant 搭建移動端項目

使用 vue3 + vite + Vant 搭建移動端項目,為了避免全量引入 vant 導致打包體積過大,又不想一個一個組件手動導入,所以就選擇了 vant 官方推薦的方法,使用 unplugin-vue-components 插件自動引入組件,并按需引入組件的樣式。

但是運行過程中遇到了報錯:

[vite] Internal server error: Failed to resolve import "vant/es" from "xxx"

vue3 + vite

項目依賴

  • package.json
{
  "name": "vue3-demo",
  "private": true,
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite --host",
    "build": "vue-tsc && vite build",
    "preview": "vite preview",
    "git": "tive git -c tive.git.config.cjs",
    "lint": "eslint --ext .js,.jsx,.ts,.tsx --fix --quiet ./src",
    "lint:stylelint": "stylelint --cache --fix \"src/**/*.{less,css,scss}\" --cache --cache-location node_modules/.cache/stylelint/",
    "prepare": "husky install"
  },
  "dependencies": {
    "amfe-flexible": "^2.2.1",
    "axios": "^1.4.0",
    "lib-flexible": "^0.3.2",
    "pinia": "^2.0.35",
    "vant": "^4.3.1",
    "vue": "^3.2.47",
    "vue-router": "4.0.1"
  },
  "devDependencies": {
    "@types/node": "^20.1.0",
    "@typescript-eslint/eslint-plugin": "^5.59.2",
    "@typescript-eslint/parser": "^5.59.2",
    "@vitejs/plugin-vue": "^4.1.0",
    "typescript": "^5.0.2",
    "unplugin-vue-components": "^0.24.1",
    "vite": "^4.3.2",
    "vite-plugin-compression": "^0.5.1",
    "vite-plugin-eslint": "^1.8.1",
    "vite-plugin-style-import": "^2.0.0",
    "vue-eslint-parser": "^9.2.1",
    "vue-tsc": "^1.4.2"
  },
  "lint-staged": {
    "*.{js,jsx,tsx,ts}": [
      "npm run lint",
      "npm run lint:stylelint"
    ]
  }
}

完整報錯

  Plugin: vite-plugin-eslint
  File: /Users/tiven/Desktop/dev/yc-chat-mbi/src/components/Footer.vue
2:47:05 PM [vite] Internal server error: Failed to resolve import "vant/es" from "src/components/Footer.vue". Does the file exist?
  Plugin: vite:import-analysis
  File: /Users/tiven/Desktop/dev/yc-chat-mbi/src/components/Footer.vue:1:89
  1  |  /* unplugin-vue-components disabled */import { Field as __unplugin_components_1 } from 'vant/es';import 'vant/es/field/style/index';
     |                                                                                          ^
  2  |  import { Button as __unplugin_components_0 } from 'vant/es';import 'vant/es/button/style/index';
  3  |  import { defineComponent as _defineComponent } from "vue";
  • vite.config.ts 配置如下:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import path from 'node:path'
import viteCompression from 'vite-plugin-compression'
import postCssPxToRem from 'postcss-pxtorem'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    // 在導入模塊時,如果模塊路徑不包含文件擴展名,則會嘗試添加下面這些擴展名
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    // 在導入模塊時,如果模塊路徑以 / 開頭,則會嘗試在下面這些目錄中查找該模塊
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@img': path.resolve(__dirname, './src/assets/img'),
    },
  },
  build: {
    sourcemap: false,
    minify: 'terser',
    assetsInlineLimit: 4096,
    reportCompressedSize: false,
    rollupOptions: {
      output: {
        // 最小化拆分包
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString()
          }
        },
        chunkFileNames: 'assets/js/[name].[hash].js', // 用于命名代碼拆分時創(chuàng)建的共享塊的輸出命名,[name]表示文件名,[hash]表示該文件內(nèi)容hash值
      },
      // external: ['antd'],
    },
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
  },
  plugins: [
    vue(),
    Components({
      resolvers: [VantResolver()],
    }),
    eslintPlugin({
      include: ['src/**/*.ts', 'src/**/*.vue', 'src/*.ts', 'src/*.vue'],
    }),
    viteCompression({
      threshold: 1024 * 4,
    }),
  ],
  css: {
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue: 37.5, //1rem的大小
          propList: ['*'], //需要轉(zhuǎn)換的屬性
          selectorBlackList: ['.norem'], //過濾掉不需要轉(zhuǎn)換的類名
        }),
      ],
    },
  },
})

解決辦法

修改 vite.config.ts 下的 resolve.extensions 參數(shù)配置,加入 .mjs 拓展名即可解決。

export default defineConfig({
  resolve: {
    // 在導入模塊時,如果模塊路徑不包含文件擴展名,則會嘗試添加下面這些擴展名
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    // 在導入模塊時,如果模塊路徑以 / 開頭,則會嘗試在下面這些目錄中查找該模塊
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@img': path.resolve(__dirname, './src/assets/img'),
    },
  },
})

以上就是解決vue3+vite配置unplugin-vue-component找不到Vant組件的詳細內(nèi)容,更多關于vue3 vite配置unplugin的資料請關注腳本之家其它相關文章!

相關文章

  • vue如何通過$router.push傳參數(shù)

    vue如何通過$router.push傳參數(shù)

    這篇文章主要介紹了vue如何通過$router.push傳參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue中添加音頻和視頻的示例詳解

    vue中添加音頻和視頻的示例詳解

    這篇文章主要為大家詳細介紹了如何vue中添加音頻和視頻的相關知識,文中的示例代碼簡潔易懂,具有一定的學習價值,感興趣的小伙伴可以了解下
    2023-08-08
  • 淺析Vue中生命周期函數(shù)的區(qū)別

    淺析Vue中生命周期函數(shù)的區(qū)別

    生命周期分為四個對子,根據(jù)不同的情況使用不同的函數(shù),這篇文章主要為大家介紹了這些函數(shù)的使用與區(qū)別,感興趣的小伙伴可以了解一下
    2023-08-08
  • Vue3 使用Vuex和router的注意事項及操作方法

    Vue3 使用Vuex和router的注意事項及操作方法

    在vue2中 使用的?this.$route?和?this.$router?this.$store的使用在vue3中完全適用,這篇文章主要介紹了Vue3 使用Vuex和router的注意事項及操作方法,需要的朋友可以參考下
    2022-12-12
  • vue雙向數(shù)據(jù)綁定指令v-model的用法

    vue雙向數(shù)據(jù)綁定指令v-model的用法

    這篇文章主要介紹了vue雙向數(shù)據(jù)綁定指令v-model的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 分享Vue組件傳值的幾種常用方式(二)

    分享Vue組件傳值的幾種常用方式(二)

    這篇文章主要介紹了分享Vue組件傳值的幾種常用方式,文章圍繞主題斬開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • vue單頁應用加百度統(tǒng)計代碼(親測有效)

    vue單頁應用加百度統(tǒng)計代碼(親測有效)

    這篇文章主要介紹了vue單頁應用加百度統(tǒng)計代碼的解決方法,需要的朋友參考下吧
    2018-01-01
  • vue實現(xiàn)登陸登出的實現(xiàn)示例

    vue實現(xiàn)登陸登出的實現(xiàn)示例

    本篇文章主要介紹了vue實現(xiàn)登陸登出的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue自定義正在加載動畫的例子

    vue自定義正在加載動畫的例子

    今天小編就為大家分享一篇vue自定義正在加載動畫的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 關于Vue3中defineProps用法圖文詳解

    關于Vue3中defineProps用法圖文詳解

    在vue3中組件傳參有很多種方式,和v2大差不差,但是有的地方還是有很多的區(qū)別,下面這篇文章主要給大家介紹了關于Vue3中defineProps用法的相關資料,需要的朋友可以參考下
    2022-11-11

最新評論