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

问题描述 投票:1回答:2

我正在努力将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)
vue.js bootstrap-4 nuxt.js bootstrap-vue
2个回答
3
投票

经过大量研究和对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文档和Vue插件的指令中找到

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

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

因为它会导入components指令内的所有内容,并且使用额外的JS代码污染你的最终捆绑包,因为它不会被正确地树木化(webpack bug),这可能会制止这种设置的全部目的,所以使用如上所述的显式导入。

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

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

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

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

0
投票

由于引入了内置的Nuxt模块,因此使用带有Nuxt的BootstrapVue的2.0.0-rc.14版本大大简化了,无需手动创建插件。为了获得与上面相同的结果,您只需要注册一个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";
© www.soinside.com 2019 - 2024. All rights reserved.