vant icon indicating copy to clipboard operation
vant copied to clipboard

NuxtJs按需加载出错

Open oneosx opened this issue 7 years ago • 6 comments

IMPORTANT: Please use the following link to create a new issue. If your issue was not created using the app above, it will be closed immediately.

注意:请按照下面的模板来新建 issue,不规范的 issue 会被立即关闭.

Describe the bug A clear description of what the bug is. 在nuxt中配置vant按需使用后,npm run dev成功,但运行时报错,无法按需加载,但是导入所有组件方式是没有问题的。

Screenshots Add screenshots to help explain your problem. vant-err

Environment

  • Device: PC
  • Browser: Chrome
  • Vant Version: "^2.0.0"

Reproduce package.json: "dependencies": { "@nuxtjs/axios": "^5.3.6", "@nuxtjs/pwa": "^2.6.0", "extract-text-webpack-plugin": "^4.0.0-beta.0", "nuxt": "^2.0.0", "vant": "^2.0.0" }, "devDependencies": { "@nuxtjs/eslint-config": "^0.0.1", "@nuxtjs/eslint-module": "^0.0.1", "@vue/test-utils": "^1.0.0-beta.27", "babel-core": "7.0.0-bridge.0", "babel-eslint": "^10.0.1", "babel-jest": "^24.1.0", "babel-plugin-import": "^1.12.0", "eslint": "^5.15.1", "eslint-config-prettier": "^4.1.0", "eslint-config-standard": ">=12.0.0", "eslint-plugin-import": ">=2.16.0", "eslint-plugin-jest": ">=22.3.0", "eslint-plugin-node": ">=8.0.1", "eslint-plugin-nuxt": ">=0.4.2", "eslint-plugin-prettier": "^3.0.1", "eslint-plugin-promise": ">=4.0.1", "eslint-plugin-standard": ">=4.0.0", "eslint-plugin-vue": "^5.2.2", "jest": "^24.1.0", "nodemon": "^1.18.9", "prettier": "^1.16.4", "vue-jest": "^3.0.3" }

nuxt.config.js:

plugins: ['@/plugins/vant-ui'] build: { extend(config, ctx) {}, babel: { plugins: [ [ 'import', { libraryName: 'vant', libraryDirectory: 'es' }, 'vant' ] ] } }

vant-ui.js:

`import Vue from 'vue' import { Button } from 'vant'

Vue.use(Button)`

oneosx avatar Jun 12 '19 03:06 oneosx

可以把 libraryDirectory: 'es' 去掉试下,这个问题的原因应该是 nuxt 的 plugin 不支持直接引入 ES module

chenjiahan avatar Jun 13 '19 08:06 chenjiahan

如果是这种问题

path/to/node_modules/vant/lib/popup/index.css:1
 SyntaxError: Unexpected token .
[0] [0]     at new Script (vm.js:51:7)
[0] [0]     at createScript (vm.js:136:10)
[0] [0]     at Object.runInThisContext (vm.js:197:10)
[0] [0]     at Module._compile (internal/modules/cjs/loader.js:618:28)
[0] [0]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
[0] [0]     at Module.load (internal/modules/cjs/loader.js:566:32)
[0] [0]     at tryModuleLoad (internal/modules/cjs/loader.js:506:12)

可以这样解决,将vant/lib/*/style里的所有文件还是都打包到bundle文件里, style文件夹里面的文件除了css/less,就剩下引入这些css/less的js文件了,所以都打包进去就没这个问题了

  externals: nodeExternals({
    whitelist: [/\.(css|scss|less)$/, /\?vue&type=style/, /vant\/lib\/\w+\/style$/]
  }),

anthinkingcoder avatar Jun 13 '19 11:06 anthinkingcoder

没试过 @anthinkingcoder 的方法,最后是这样解决的。

plugins/vant-ui.js

image

nuxt.config.js:

image

oneosx avatar Jun 15 '19 06:06 oneosx

@AppxLite 这样相当需加载全部组件的样式了吧,

luchanan avatar Aug 06 '19 07:08 luchanan

我最近也在用nuxt, 按需加载已搞定,请看我的文章 https://juejin.im/post/5e012778518825127f085da8

gp0320 avatar Dec 25 '19 16:12 gp0320

没试过 @anthinkingcoder 的方法,最后是这样解决的。

plugins/vant-ui.js

image

nuxt.config.js:

image

你这样还是全部加载。babel.plugins那段配置去掉也能生效。

Griffin23 avatar Mar 16 '20 03:03 Griffin23