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的更多相关文章

  1. [Vue] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  2. vue报错[Vue warn]: Unknown custom element: <router-Link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    vue浏览器报错,如下 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <router-Link> - di ...

  3. 前端框架vue.js系列(9):Vue.extend、Vue.component与new Vue

    前端框架vue.js系列(9):Vue.extend.Vue.component与new Vue 本文链接:https://blog.csdn.net/zeping891103/article/det ...

  4. Vue报错之" [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component correctly? For recursive components, make sure to provide the "name" option."

    一.报错截图 [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component corre ...

  5. [Vue @Component] Extend Vue Components in TypeScript

    This lesson shows how you can extend and reuse logic in Vue components using TypeScript inheritance. ...

  6. [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component

    Components with slots can expose their data by passing it into the slot and exposing the data using  ...

  7. [Vue @Component] Load Vue Async Components

    Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...

  8. 使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the "name" option.(未注册组件)的原因之一

    错误信息: [Vue warn]: Unknown custom element: <list> - did you register the component correctly? F ...

  9. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

随机推荐

  1. JavaScript Json与Map互转以及Map对象的取值方式

    Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...

  2. 生成100个Div

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  3. 清除浮动(float)的影响

    浮动会导致父元素塌陷如图: 解决办法: 父元素overflow:hidden,如图 末尾插入子元素clear,如图 为甚么,父元素overflow:hidden会解决塌陷问题? 来自知乎貘吃馍香的回答 ...

  4. C++ 模板template和template

    原文链接:https://blog.csdn.net/skyleung/article/details/42195509 template<class T>和template<typ ...

  5. 8月中旬之后的学习计划 --- react

    这段时间快活了,放纵了,玩hi了,接下来该好好的学习新知识了. 鉴于目前业界比较火的前端js框架有react.vue,决定先从react开始学习.之前有简单的接触过它的一些基本的语法知识,这次准备全面 ...

  6. Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout

    automaticallyAdjustsScrollViewInsets:在导航栏透明时用到 In your viewDidLoad method, add if([self respondsToSe ...

  7. vue基础---表单输入绑定

    [一]基础用法 用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定.它会根据控件类型自动选取正确的方 ...

  8. 如何在MONO 3D寻找最短路路径

    前段时间有个客户说他们想在我们的3D的机房中找从A点到B点的最短路径,然而在2D中确实有很多成熟的寻路算法,其中A*是最为常见的,而这个Demo也是用的A*算法,以下计算的是从左上角到右下角的最短路径 ...

  9. 我已经迷失在事件环(event-loop)中了【Nodejs篇】

    我第一次看到他事件环(event-loop)的时候,我是一脸懵,这是什么鬼,是什么循环吗,为什么event还要loop,不是都是一次性的吗? 浏览器中和nodejs环境中的事件环是有一些区别的,这里我 ...

  10. localStorage前端存储数据

    <!DOCTYPE html> <html> <head> <title>localStorage演示</title> <meta c ...