angular裏使用vue/vue組件怎麼在angular裏用

歡迎加入前端交流羣交流知識&&獲取視頻資料:749539640

如何在angularjs(1)中使用vue

參考: https://medium.com/@graphicbeacon/how-to-use-vue-2-0-components-in-an-angular-application-4d85bacc42dc

如何在angular2-7中使用vue

將Vue組件包裝爲本Web組件css

因爲Angular支持使用自定義Web組件,所以可以使用Vue組件(包裝爲Web組件)。html

對於Angular,若是自定義Web組件是由Vue生成的,那麼它就沒有區別(對於全部Angular都知道,它們能夠是本機HTML元素)前端

這裏使用element-ui做爲組件導入angular使用vue

demo地址https://stackblitz.com/edit/angular-nnbszr
git

index.htmlangularjs

<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.tsgithub

如今,在angular裏,導入Web組件後(即他們Vue的產生與否),其配置爲使用加入schemas: [CUSTOM_ELEMENTS_SCHEMA]@NgModuleweb

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.htmlelement-ui

如今直接在Angular模板中使用Web組件(從Vue生成或不生成)。例如,上面代碼中定義的組件能夠像如下同樣使用:bootstrap

<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文檔

相關文章
相關標籤/搜索