在 Angular 中使用 Vue 组件 [英] Using Vue Components in Angular
问题描述
我有一个用 Vue 构建的项目,我想在 Angular 应用程序中重用 Vue 应用程序中的组件,这样我就不必从头开始重新构建每个组件.
我在媒体上看到了这个教程:如何在 Angular 应用程序中使用 Vue 2.0 组件,但该教程适用于 AngularJS.
我想知道以前是否有人这样做过,是否值得,以及是否有人知道任何教程或参考资料.
将您的 Vue 组件包装为 native Web组件.
由于 Angular 支持使用自定义 Web 组件,您将能够使用 Vue 组件(包装为 Web 组件).
对于 Angular 来说,自定义 Web 组件是否由 Vue 生成并没有什么区别(因为所有 Angular 都知道,它们可能是原生 HTML 元素).
演示
该演示是一个 Angular 5 应用程序.Vue 自定义组件定义在 index.html
一>.请注意 app/app.component.html
直接在模板中使用,就像原生元素一样.
下面一步一步.
在 Vue 中
使用 vue-custom-element
将您的 Vue 组件包装为 Web 组件:
<script src="https://unpkg.com/vue-custom-element@3.0.0/dist/vue-custom-element.js"></script><脚本>const MyVueWebComp = {道具:['味精'],模板:`<div style="border: 3px 绿色虚线;padding: 5px">我是 my-vue-web-comp.
"msg" 属性的值:{{ msg }}
<input v-model="text"><button @click="addText">点击我</button><div v-for="t in texts">文字:{{ t }}
`,数据() {返回 {文本: '',文字:[]};},方法: {添加文字() {this.texts.push(this.text);this.text = '';}}};Vue.customElement('my-vue-web-comp', MyVueWebComp);
这将创建一个 <my-vue-web-comp>
网络组件,可以直接在 DOM 中使用,无需需要有一个可用的 Vue实例.
以上只是一个可直接在浏览器中运行的演示.如果您有 .vue 文件和 vue-cli 应用程序,您需要执行 npm install vue-custom-element --save
然后创建一个 .js
文件如:
从'vue'导入Vue;从 'vue-custom-element' 导入 vueCustomElement;从'./MyElement.vue'导入MyElement;Vue.use(vueCustomElement);Vue.customElement('my-element', MyElement);
然后,当捆绑时,将生成一个 .js
文件,该文件可以作为单个 标签直接导入,而不是 上面的完整代码和脚本标签.
更多详情,请查看vue-custom-element
's 文档.
在角度
现在,在 Angular 应用程序中,在导入 Web 组件(无论它们是否由 Vue 生成)后,通过在 @NgModule
中添加 schemas: [CUSTOM_ELEMENTS_SCHEMA]
来配置它们以供 Angular 使用:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';//...@NgModule({//...模式:[CUSTOM_ELEMENTS_SCHEMA//添加了这个]})导出类 AppModule {
现在直接在 Angular 模板中使用 Web 组件(无论是否由 Vue 生成).例如.上面代码中定义的组件可以像这样使用:
<my-vue-web-comp [msg]="name"></my-vue-web-comp>
事实上,可运行的演示 展示了这种用法的一个例子.
限制
您可能需要 polyfills 来支持旧版浏览器.请查看 vue-custom-element
的文档了解更多详情.
I have a project that is built in Vue and I want to reuse the components from the Vue application in an Angular application so I don't have to go and rebuild every single component from scratch.
I saw this tutorial on medium: How to use Vue 2.0 components in an angular application, but that tutorial is for AngularJS.
I'm wondering if anyone has done this before, if it's worth it and if anyone knows of any tutorials or reference material.
Wrap your Vue components as native Web Components.
Since Angular supports using custom Web Components, you'll be able to use the Vue components (wrapped as Web Components).
To Angular it doesn't make a difference if the custom Web Components were generated by Vue or not (for all Angular knows, they could be native HTML elements).
Demo
The demo is an Angular 5 app. The Vue custom component is defined in index.html
. Notice how in app/app.component.html
it is used directly in the template, as if it were a native element.
Step by step below.
In Vue
Use vue-custom-element
to wrap your Vue components as Web Components:
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-custom-element@3.0.0/dist/vue-custom-element.js"></script>
<script>
const MyVueWebComp = {
props: ['msg'],
template:`
<div style="border: 3px dashed green; padding: 5px">
I am my-vue-web-comp.<br>
Value of "msg" prop: {{ msg }}<br>
<input v-model="text"><button @click="addText">Click me</button>
<div v-for="t in texts">
Text: {{ t }}
</div>
</div>
`,
data() {
return {
text: '',
texts: []
};
},
methods: {
addText() {
this.texts.push(this.text);
this.text = '';
}
}
};
Vue.customElement('my-vue-web-comp', MyVueWebComp);
</script>
That will create a <my-vue-web-comp>
web component that can be used directly in the DOM, without the need to have a working Vue instance.
The above is just a demo runnable directly in the browser. If you have .vue files and a vue-cli app, you'll need to do npm install vue-custom-element --save
and then create a .js
file like:
import Vue from 'vue';
import vueCustomElement from 'vue-custom-element';
import MyElement from './MyElement.vue';
Vue.use(vueCustomElement);
Vue.customElement('my-element', MyElement);
And then this, when bundled, will generate a .js
file that can be imported directly as a single <script>
tag, instead of the whole code and script tags above.
For more details, check vue-custom-element
's docs.
In Angular
Now, in the Angular app, after importing the Web Components (being them Vue-generated or not), configure them to be used by Angular by adding schemas: [CUSTOM_ELEMENTS_SCHEMA]
in your @NgModule
:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
//...
@NgModule({
// ...
schemas: [
CUSTOM_ELEMENTS_SCHEMA // added this
]
})
export class AppModule {
Now use the Web Components (generated from Vue or not) directly in Angular templates. E.g. the component defined in the code above could be used like:
<my-vue-web-comp [msg]="name"></my-vue-web-comp>
In fact, the runnable demo shows an example of that usage.
Limitations
You may need polyfills for older browser support. Please check vue-custom-element
's docs for more details.
这篇关于在 Angular 中使用 Vue 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!