将Vue组件包装为本Web组件。
由于Angular支持使用自定义Web组件,因此能够使用Vue组件(包装为Web组件)。
对于Angular,如果自定义Web组件是由Vue生成的,那么它就没有区别(对于所有Angular都知道,它们可以是本机HTML元素)
我们使用vue-custom-element来来进行包装
demo地址:这里使用element-ui作为组件导入angular使用
代码地址
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-custom-element@3.0.0/dist/vue-custom-element.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
const MyVueWebComp = {
props: ['msg'],
template:`
<div style="border: 3px dashed green; padding: 5px">
I am my-vue-web-comp.<br>
Value received via "msg" prop: {{ msg }}<br>
<input v-model="text"><button @click="addText">Type something and click me</button>
<div v-for="t in texts">
Text: {{ t }}
</div>
<div>
<el-button @click="show()" type="danger">Button</el-button>
<el-dialog :visible.sync="visible" title="Hello world">
<p>我是vue Element 组件</p>
</el-dialog>
</div>
</div>
`,
data() {
return {
text: '',
texts: [],
visible : false
};
},
methods: {
addText() {
this.texts.push(this.text);
this.text = '';
},
show() {
this.visible = true;
}
}
};
Vue.customElement('my-vue-web-comp', MyVueWebComp);
</script>
<my-app>loading</my-app>
如果是ts内使用(同样vue.js也是再index.html引入)
declare var Vue: any;
现在,在angular里,导入Web组件后,其配置为使用加入schemas: [CUSTOM_ELEMENTS_SCHEMA]:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
schemas: [
CUSTOM_ELEMENTS_SCHEMA // Added for custom elements support
]
})
export class AppModule { }
现在直接在Angular模板中使用Web组件(从Vue生成或不生成)。例如,上面代码中定义的组件可以像以下一样使用:
<h3>Hello, {{ name }}</h3>
<p>In index.html, a Vue component is defined using Vue and is wrapped into a Web Component using vue-custom-element.<br>
Now, in Angular's template, use it as if it were a native HTML Element (or regular native Web Component).
</p>
<my-vue-web-comp [msg]="name"></my-vue-web-comp>
有关更多详细信息,请查看vue-custom-element文档。
参考
背景 在公司参与一个原生APP和h5混合开发的项目,本人在项目中负责h5部分,现将...
作者:Joe Seifi 译者:前端小智 移动: https://mp.weixin.qq.com/s/p5... 有梦...
1、纯工具操作步骤,懂代码更容易 划线就是不符合国人的审美观念,看着就别扭,...
1.先瞧瞧效果: 2.代码是这样的: img src=images/circle.png alt= id=circle/@m...
简介: 6月4日,以“开启分布式云新时代”为主题2021云边协同大会在北京举行,本...
前言 Canvas绘制多变形非常简单,只要懂得Canvas路径 + 简单的初中数学知识即可...
在默认情况之下,如果在Canvas之中将某个物体(源)绘制在另一个物体(目标)之...
折叠式卡片布局在PC版网站中可能不常见,但是在手机版,小屏幕的网页浏览会大发...
本文没有咬文嚼字的地方,只是一个配色技巧的分享,十分简单,简单到流泪,但或...
一、虽然有的属性是boolean类型,但仍旧建议按照XHTML书写(属性名=属性值)格式...