Nuxt.js + Bootstrap-Vue - 单独的组件和指令加载

Nuxt.js + Bootstrap-Vue - Individual components and directives loading

我正在努力将 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)

经过大量研究并对 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)

我们将只导入那些我们想要使用的组件。有关更多信息,请参见 bt-vue 文档 Component groups and Directives as Vue plugins

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

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 的引用,因为它是单独提供的。在我的设置中,我直接从常规 bootstrap 包中导入 SASS 文件到我的 index.scss 文件中,并像往常一样将其包含在 nuxt.config.js 中。

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

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

中的一些特殊设置

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";