简体   繁体   English

如何在 vue 应用程序中使用独立的 vue 库?

[英]How can I use standalone vue library in a vue application?

I am trying to use a vue component library, which is loaded by a script tag, within a vue application.我正在尝试在 vue 应用程序中使用由脚本标记加载的 vue 组件库。 That component library is using a webpack setup and uses the setting externals to exclude all vue dependencies.该组件库使用 webpack 设置并使用externals设置来排除所有 vue 依赖项。 So the host bundle which is using that library has to ship vue and other needed dependencies.因此,使用该库的主机包必须提供 vue 和其他所需的依赖项。 The component library itself is using vue cli with the script vue-cli-service build --target lib to build a lib.umd.js file.组件库本身使用 vue cli 和脚本vue-cli-service build --target lib来构建lib.umd.js文件。 Inside the index.html of my host bundle I am just including that file by a script tag.在我的主机包的index.html ,我只是通过脚本标记包含该文件。 The webpack setup of the host bundle uses config.externals(['@test/my-component-library']) to exclude the component library which is being loaded externally.主机包的 webpack 设置使用config.externals(['@test/my-component-library'])排除外部加载的组件库。 Inside the host bundle I am using the component library like this:在主机包中,我正在使用这样的组件库:

import { MyComponent } from '@test/my-component-library'

When I run my application, I get the following error: Uncaught SyntaxError: Invalid or unexpected token .当我运行我的应用程序时,我收到以下错误: Uncaught SyntaxError: Invalid or unexpected token This error happens in the following line of my host bundle:此错误发生在我的主机包的以下行中:

/*!*******************************************************!*\
  !*** external "@test/my-component-library" ***!
  \*******************************************************/
/*! no static exports found */
/***/ (function(module, exports) {
eval("module.exports = @test/my-component-library;//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQHRlc3QvbXktY29tcG9uZW50LWxpYnJhcnkuanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vZXh0ZXJuYWwgXCJAdGVzdC9teS1jb21wb25lbnQtbGlicmFyeVwiPzFjOTAiXSwic291cmNlc0NvbnRlbnQiOlsibW9kdWxlLmV4cG9ydHMgPSBAdGVzdC9teS1jb21wb25lbnQtbGlicmFyeTsiXSwibWFwcGluZ3MiOiJBQUFBIiwic291cmNlUm9vdCI6IiJ9\n//# sourceURL=webpack-internal:///@test/my-component-library\n");
/***/ })

Does anyone know why webpack is producing such code and how I can correct it?有谁知道为什么 webpack 会生成这样的代码以及我如何纠正它?

I guess I found the solution.我想我找到了解决方案。 So first of all I had a wrong configuration for externals .所以首先我对externals有一个错误的配置。 The correct configuration is:正确的配置是:

config.externals({
  '@test/my-component-library': {
    root: '@test/my-component-library',
    commonjs2: '@test/my-component-library',
    commonjs: '@test/my-component-library',
    amd: '@test/my-component-library',
  },
})

In the component library I had to add the webpack configuration config.output.library('MyComponentLibrary') so that the object MyComponentLibrary is globally available in window scope.在组件库中,我必须添加 webpack 配置config.output.library('MyComponentLibrary')以便对象MyComponentLibrary在窗口范围内全局可用。 Then I had to remove the import statement in the host bundle.然后我不得不删除主机包中的导入语句。 To register the component I had to add this line to my vue file:要注册组件,我必须将此行添加到我的 vue 文件中:

components: {
  'MyComponent': MyComponentLibrary.MyComponent,
}

But now I have another problem.但现在我有另一个问题。 The component library itself is using vuetify (which is another component library).组件库本身正在使用 vuetify(这是另一个组件库)。 But I have also added vuetify to the externals option because my host bundle already contains vuetify.但我也将 vuetify 添加到externals选项,因为我的主机包已经包含 vuetify。 And now I am getting lots of Unknown custom element exceptions.现在我收到了很多Unknown custom element异常。 So apparently my component library does not find the vuetify components in my host bundle.所以显然我的组件库在我的主机包中找不到 vuetify 组件。 Are there any solutions for this problem?这个问题有什么解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM