[Vue @Component] Dynamic Vue.js Components with the component element
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Vue @Component] Dynamic Vue.js Components with the component element相关的知识,希望对你有一定的参考价值。
You can dynamically switch between components in a template by using the reserved <component>
element and dynamically bind to its is
attribute. By using <keep-alive>
you can tell Vue to keep the component in memory.
In the previous post about dynamic component
<component :is="selectedComp"></component> <script> import Vue from "vue" import { Component, Prop } from "vue-property-decorator" const One = { functional: true, name: "One", render: h => <h1 class="bg-red">One</h1> } const Two = { functional: true, name: "Two", render: h => <h1 class="bg-green">Two</h1> } const Three = { functional: true, name: "Three", render: h => <h1 class="bg-purple">Three</h1> } @Component({ components: { } }) export default class App extends Vue { comps = [One, Two, Three] selectedComp = this.comps[0] } </script>
Because inside @Component({components: {}}) haven\'t register those component \'One, Two, Three\', so then using
<component :is="selectedComp"></component>
\'selectedComp\' has to ben a real functional component.
But If we have registered the components:
@Component({
components: {
One, Two, Three
}
})
The the \'selectedComp\' can be just component\'s name:
selectedComponent = \'One\' | \'Two\' | \'Three\'
<keep-alive> Component:
Components might have some state, you want to keep the state instead of losting it, you can use \'keep-alive\' component:
<keep-alive> <component v-bind:is="currentView" v-bind:initial-quantity="item.quantity" v-bind:name="item.text" v-bind:diet="item.diet"></component> </keep-alive>
以上是关于[Vue @Component] Dynamic Vue.js Components with the component element的主要内容,如果未能解决你的问题,请参考以下文章
[Vue] Lazy Load a Route by using the Dynamic Import in Vue.js
Vue Dynamic Layouts 两次挂载路由器视图组件
[Vue @Component] Simplify Vue Components with vue-class-component