【问题标题】:Nuxt.js + Bootstrap-Vue - Individual components and directives loadingNuxt.js + Bootstrap-Vue - 加载单个组件和指令
【发布时间】:2018-12-17 11:16:25
【问题描述】:

我正在努力将 Bootstrap-Vue 库集成到我的基于 Nuxt.js 的项目中。我通读了official documentation 以开始使用,但是尽管将 bt-vue 作为单个模块导入工作正常,但我希望能够导入单个组件和指令以减小生成的文件大小并使我的设置尽可能高效。文档仅针对该主题的常规 Vue.js 项目提供了解决方案,但我如何编写一个插件来使我能够对 Nuxt 做同样的事情?

我开始像这样创建一个bt-vue.ts 插件:

import Vue from 'vue'
import { Card } from 'bootstrap-vue/es/components';

Vue.use(Card);

我已将此文件导入到 nuxt.config.js 插件部分

plugins: [
...
'@/plugins/bt-vue'
...
]

但是当我尝试编译我的项目时,我收到了这个错误:

node_modules\bootstrap-vue\es\components\index.js:1
  (function (exports, require, module, __filename, __dirname) { import Alert from './alert';
  ^^^^^^

  SyntaxError: Unexpected token import
  at createScript (vm.js:80:10)
  at Object.runInThisContext (vm.js:139:10)
  at Module._compile (module.js:616:28)
  at Object.Module._extensions..js (module.js:663:10)
  at Module.load (module.js:565:32)
  at tryModuleLoad (module.js:505:12)
  at Function.Module._load (module.js:497:3)
  at Module.require (module.js:596:17)
  at require (internal/module.js:11:18)
  at r (C:\Projects\Wonder\frontend-nuxt\node_modules\vue-server-renderer\build.js:8330:16)
  at Object.bootstrap-vue/es/components (server-bundle.js:5771:18)
  at __webpack_require__ (webpack/bootstrap:25:0)
  at Module../plugins/bt-vue/index.ts (plugins/bt-vue/index.ts:1:0)
  at __webpack_require__ (webpack/bootstrap:25:0)
  at Module../.nuxt/index.js (.nuxt/index.js:1:0)
  at __webpack_require__ (webpack/bootstrap:25:0)

【问题讨论】:

  • 你能从 clarcdo 试试这个解决方案吗:向外部添加 dom-classes ---- const nodeExternals = require('webpack-node-externals') module.exports = { build: { extend(config , ctx) { if (ctx.isServer) { config.externals = [ nodeExternals({ whitelist: [/^dom-classes/] }) ] } } } }
  • 什么是 dom 类?我应该为此创建一个单独的模块吗?是否可以与 Webpack 4 一起使用?也许我可以使用 extendBuild 功能?

标签: vue.js bootstrap-4 nuxt.js bootstrap-vue


【解决方案1】:

经过大量研究和对 bt-vue lib 的一些修复后,我找到了应对这一挑战的解决方案。 此解决方案适用于 Nuxt 2,而 不会与 Nuxt 1 一起使用: 首先你需要创建一个插件:

import Vue from 'vue'
import Collapse from 'bootstrap-vue/es/components/collapse'
import Dropdown from 'bootstrap-vue/es/components/dropdown'

Vue.use(Collapse)
Vue.use(Dropdown)

我们将只导入我们想要使用的那些组件。更多信息可以在 Component groups and Directives as Vue plugins

下的 bt-vue 文档中找到

警告:我建议远离这种导入语法:

import { Modal } from 'bootstrap-vue/es/components';

因为它无论如何都会导入 components 指令中的所有内容,并用额外的 JS 代码污染你的最终包,因为它不会正确地摇树(webpack 错误),这可能会破坏这种设置的全部目的,所以使用如上所述的显式导入。

然后在 nuxt.config.js 中连接:

export default {
  build: {
    transpile: ['bootstrap-vue']
  },
  plugins: ['@/plugins/bt-vue']
}

正如您所见,不需要包含模块,因为我们自己编写插件,因此 SSR 没有问题!我们正在使用新的 Nuxt 2 transpile 属性来构建我们的 es6 bt-vue 模块。不要忘记包含对 css 的引用,因为它是单独提供的。在我的设置中,我只是将常规引导程序包中的 SASS 文件直接导入到我的 index.scss 文件中,然后像往常一样将其包含在 nuxt.config.js 中。

css: [
    '@/assets/scss/index.scss'
  ]

【讨论】:

  • 插件导入开始时需要import Vue from 'vue'的小提醒
  • 它在那里,不是吗?
【解决方案2】:

从 2.0.0-rc.14 版开始,使用带有 Nuxt 的 BootstrapVue 得到了极大的简化,因为引入了内置的 Nuxt 模块,无需手动创建插件。 为了获得与上面相同的结果,您只需要在 nuxt.config.js 中注册一个 bt-vue 模块以及一些特殊设置:

modules: ['bootstrap-vue/nuxt'],
bootstrapVue: {
    bootstrapCSS: false, // here you can disable automatic bootstrapCSS in case you are loading it yourself using sass
    bootstrapVueCSS: false, // CSS that is specific to bootstrapVue components can also be disabled. That way you won't load css for modules that you don't use
    componentPlugins: ['Collapse', 'Dropdown'], // Here you can specify which components you want to load and use
    directivePlugins: [] // Here you can specify which directives you want to load and use. Look into official docs to get a list of what's available
  }

如果您对如何加载特定 bt-vue 组件的 scss 感到好奇:

@import "~bootstrap-vue/src/variables";
// All bt-vue styles can be imported with this reference
//@import "~bootstrap-vue/src/components/index";

// Importing only styles for components we currently use
@import "~bootstrap-vue/src/components/dropdown/index";

【讨论】:

  • 最新的 BootstrapVue nuxt 插件现在允许导入单个组件和指令。
猜你喜欢
  • 2021-04-23
  • 2020-08-02
  • 2020-07-12
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 2014-01-07
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多