分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

[Vue @Component] Dynamic Vue.js Components with the component element

发布时间:2023-09-06 02:06责任编辑:蔡小小关键词:jselement

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

原文地址:https://www.cnblogs.com/Answer1215/p/9374566.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved