博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular里使用vue/vue组件怎么在angular里用
阅读量:6913 次
发布时间:2019-06-27

本文共 2263 字,大约阅读时间需要 7 分钟。

交流知识&&获取视频资料:

如何在angularjs(1)中使用vue参考: 

如何在angular2-7中使用vue

将Vue组件包装为本。

由于Angular支持使用自定义Web组件,因此能够使用Vue组件(包装为Web组件)。

对于Angular,如果自定义Web组件是由Vue生成的,那么它就没有区别(对于所有Angular都知道,它们可以是本机HTML元素)

这里使用element-ui作为组件导入angular使用

demo地址

index.html

<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;

app.module.ts

现在,在angular里,导入Web组件后(即他们Vue的产生与否),加入schemas: [CUSTOM_ELEMENTS_SCHEMA]@NgModule

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 { }

app.component.html

现在直接在Angular模板中使用Web组件(从Vue生成或不生成)。例如,上面代码中定义的组件可以像以下一样使用:

Hello, {
{ name }}

In index.html, a Vue component is defined using Vue and is wrapped into a Web Component using vue-custom-element.

Now, in Angular's template, use it as if it were a native HTML Element (or regular native Web Component).

 

有关更多详细信息,请查看。

转载于:https://www.cnblogs.com/wangzhichao/p/10616120.html

你可能感兴趣的文章
python爬虫 urllib2
查看>>
菜鸟入门级:SQL注入***
查看>>
JAVA7新特性
查看>>
我的友情链接
查看>>
安装vsftp
查看>>
数据库同步产品PAC市场定位
查看>>
Linux 用户管理
查看>>
搭建open***
查看>>
哪个会话引起阻塞并且它们在运行什么
查看>>
各个版本的金蝶kis的区别
查看>>
JS闭包的理解
查看>>
Linux下查看操作系统信息、内存情况及cpu信息
查看>>
影像拍攝及呈現能力的提升
查看>>
Windows Server 2012 Hyper-V新特性(11)
查看>>
Android快速开发系列 10个常用工具类
查看>>
我的友情链接
查看>>
1. 引用类型的类型转换
查看>>
关于linux hrtimer高精度定时器的使用注意事项
查看>>
高清视频教程网站的搭建和分享
查看>>
Android 混淆代码总结
查看>>