简体   繁体   English

如何使用 Vue CLI 一次将多个 Vue.js 组件构建为原生 Web 组件?

[英]How can I build multiple Vue.js components to Native Web Components using Vue CLI at once?

I'm new to Vue.js and want to make an SDK of Web Components using it.我是 Vue.js 的新手,想使用它制作 Web 组件的 SDK。 I can make one single web component like this:我可以像这样制作一个 web 组件:

main.js:主.js:

import Vue from 'vue';
import App from './App.vue';
import wrap from '@vue/web-component-wrapper';

const customElement = wrap(Vue, App);

window.customElements.define('sky-chat', customElement);

Cli command:命令行命令:

vue build --target wc --name sky-chat ./src/main.js

But this is just for one single component.但这仅适用于单个组件。 How can I build a bunch of.vue files to separate web components in separate js files at once?如何构建一堆 .vue 文件以一次将 web 组件分离到单独的 js 文件中?

How can I generate a test html automatically containing all components inside it for testing in dist folder after building?如何生成测试 html 自动包含其中的所有组件,以便在构建后在 dist 文件夹中进行测试?

Thanks谢谢

Version 4.3.0 added support for specifying multiple web components to be built via comma-separated glob patterns. 4.3.0 版增加了对指定多个 web 组件以通过逗号分隔的全局模式构建的支持。

Examples:例子:

vue-cli-service build --target wc --name myprefix src/components/core*.vue,src/components/basic*.vue

vue-cli-service build --target wc --name myprefix src/components/myFirst.vue,src/components/mySecond.vue

This produces an individual web component for each entry point specified, and a single demo.html that contains all components.这将为每个指定的入口点生成一个单独的 web 组件,以及一个包含所有组件的demo.html However, there doesn't seem to be support for separate .js files per web component.但是,似乎不支持每个 web 组件的单独.js文件。 The web components are bundled into a single file. web 组件捆绑到一个文件中。

You can create a vue plugin to bundle vue files together.您可以创建一个 vue 插件来将 vue 文件捆绑在一起。

// plugin.js

// your components
import Component1 from '/src/components/Component1.vue'
import Component2 from '/src/components/Component2.vue'
import Component3 from '/src/components/Component3.vue'

// This exports the plugin object.
export default {
  install (Vue, options) {
     // Add a component or directive to your plugin, so it will be installed globally to your project.
     Vue.component('component1', Component1)
     Vue.component('component2', Component2)
     Vue.component('component3', Component3)
  }
}


// your main vue file:

import myPlugin from './plugin.js'
Vue.use(myPlugin);

new Vue({
//...
})

Now you can use all the components you included in the plugin wherever you want without having to import them in each file.现在,您可以在任何地方使用插件中包含的所有组件,而无需在每个文件中导入它们。

Did I understand your question correctly?我是否正确理解了您的问题?

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

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