最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

使用 vue3 + vite + Vant 搭建移動(dòng)端項(xiàng)目

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

但是運(yùn)行過程中遇到了報(bào)錯(cuò):

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

vue3 + vite

項(xiàng)目依賴

  • 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"
    ]
  }
}

完整報(bào)錯(cuò)

  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: {
    // 在導(dǎo)入模塊時(shí),如果模塊路徑不包含文件擴(kuò)展名,則會(huì)嘗試添加下面這些擴(kuò)展名
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    // 在導(dǎo)入模塊時(shí),如果模塊路徑以 / 開頭,則會(huì)嘗試在下面這些目錄中查找該模塊
    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', // 用于命名代碼拆分時(shí)創(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: {
    // 在導(dǎo)入模塊時(shí),如果模塊路徑不包含文件擴(kuò)展名,則會(huì)嘗試添加下面這些擴(kuò)展名
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    // 在導(dǎo)入模塊時(shí),如果模塊路徑以 / 開頭,則會(huì)嘗試在下面這些目錄中查找該模塊
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@img': path.resolve(__dirname, './src/assets/img'),
    },
  },
})

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

相關(guān)文章

  • axios對(duì)請(qǐng)求各種異常情況處理的封裝方法

    axios對(duì)請(qǐng)求各種異常情況處理的封裝方法

    今天小編就為大家分享一篇axios對(duì)請(qǐng)求各種異常情況處理的封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue使用html2canvas實(shí)現(xiàn)截取圖片并保存

    Vue使用html2canvas實(shí)現(xiàn)截取圖片并保存

    html2canvas是一個(gè)JavaScript庫,它可以將HTML元素轉(zhuǎn)換為Canvas元素本文將介紹一下Vue如何使用html2canvas實(shí)現(xiàn)截取圖片并保存功能,需要的可以參考下
    2023-12-12
  • 淺析vue-router原理

    淺析vue-router原理

    這篇文章主要圍繞Vue的SPA單頁面設(shè)計(jì)展開。SPA(single page application):單一頁面應(yīng)用程序,有且只有一個(gè)完整的頁面,對(duì)vue router原理感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • vue實(shí)現(xiàn)下拉框篩選表格數(shù)據(jù)

    vue實(shí)現(xiàn)下拉框篩選表格數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)下拉框篩選表格數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 修改vue+webpack run build的路徑方法

    修改vue+webpack run build的路徑方法

    今天小編就為大家分享一篇修改vue+webpack run build的路徑方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue自定義封裝按鈕組件

    vue自定義封裝按鈕組件

    這篇文章主要為大家詳細(xì)介紹了vue自定義封裝按鈕組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)

    vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)

    這篇文章主要為大家介紹了vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Vue3實(shí)現(xiàn)Message消息組件示例

    Vue3實(shí)現(xiàn)Message消息組件示例

    在大多數(shù) web 產(chǎn)品中,全局的 Message 組件占有較大的使用場(chǎng)景,本文主要介紹了Vue3實(shí)現(xiàn)Message消息組件示例,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報(bào)錯(cuò)的問題

    vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報(bào)錯(cuò)的問題

    這篇文章主要介紹了vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)

    vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)

    這篇文章主要介紹了vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04

最新評(píng)論

称多县| 西畴县| 南木林县| 玉溪市| 东方市| 齐河县| 嘉鱼县| 梁平县| 华阴市| 化隆| 二连浩特市| 洛南县| 图木舒克市| 葵青区| 霞浦县| 定南县| 普安县| 漳平市| 滦平县| 抚州市| 成安县| 贵溪市| 民乐县| 三都| 阿瓦提县| 普兰县| 开原市| 滕州市| 安塞县| 五峰| 班玛县| 永丰县| 翁源县| 大丰市| 刚察县| 许昌市| 凤山市| 余干县| 宣武区| 中牟县| 定兴县|