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.

Two children components:

<!-- show.vue -->

<template>
<div>
Show Component: <span>{{name}}</span>
</div>
</template>
<script>
export default{
name: 'show-component',
props: ['name']
}
</script>
<!-- edit-->
<template>
<div>
Edit component: <input type="text" v-model="editValue"/>
</div>
</template>
<script>
export default{
name: 'edit-component',
data(){
return{
editValue: this.name
}
},
props: ['name']
}
</script>

Parent:

<template>

    <section class="container">
<section>
<h3>Dynamic component</h3>
<a @click="toggle">{{toggleButton}}</a>
<keep-alive>
<component
v-bind:is="currentView"
v-bind:name="message"
>
</component>
</keep-alive> </section>
</section>
</template> <style scoped>
.title
{
margin: 50px 0;
} </style> <script>
import ShowComponent from '../components/show';
import EditComponent from '../components/edit'; export default {
data() {
return {
message: 'this is my vue!',
currentView: "ShowComponent"
}
}, computed: {
toggleButton: function() {
return this.currentView === "ShowComponent" ?
'show': 'Edit';
}
},
components: {
EditComponent,
ShowComponent
},
methods: {
toggle() {
this.currentView =
this.currentView === "ShowComponent" ?
"EditComponent" : "ShowComponent";
}
}
} </script>

[Vue] Dynamic Vue.js Components with the component element的更多相关文章

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

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

  2. vue & dynamic components

    vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...

  3. Vue dynamic component All In One

    Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...

  4. 前端笔记之Vue(七)Vue-router&axios&Vue插件&Mock.js&cookie|session&加密

    一.Vue-router(路由) 1.1路由创建 官网:https://router.vuejs.org/zh/ 用 Vue.js + Vue Router 创建单页应用,是非常简单的.使用 Vue. ...

  5. Vue.mixin Vue.extend(Vue.component)的原理与区别

    1.本文将讲述 方法 Vue.extend Vue.mixin 与 new Vue({mixins:[], extend:{}})的区别与原理 先回顾一下 Vue.mixin 官网如下描述: Vue. ...

  6. Vue Vue.use() / Vue.component / router-view

    Vue.use Vue.use 的作用是安装插件 Vue.use 接收一个参数 如果这个参数是函数的话,Vue.use 直接调用这个函数注册组件 如果这个参数是对象的话,Vue.use 将调用 ins ...

  7. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十五 ║Vue基础:JS面向对象&字面量& this字

    缘起 书接上文<从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史>,昨天咱们说到了以我的经历说明的web开发经历的 ...

  8. vue中的js引入图片,必须require进来

    需求:如何components里面的index.vue怎样能把assets里面的图片拿出来. 1.在img标签里面直接写上路径: <img src="../assets/a1.png& ...

  9. require.js 加载 vue组件 r.js 合并压缩

    https://www.taoquns.com 自己搭的个人博客 require.js 参考阮一峰 Javascript模块化编程(三):require.js的用法 r.js 合并压缩 参考司徒正美 ...

随机推荐

  1. Android抖动的EditText

    Java代码:启动动画 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw ...

  2. 不重新启动VMWare虚拟机加入虚拟磁盘的方法(上)

    近期因为业务须要在不重新启动系统的前提下对系统进行扩容,前提是该系统做过lvm.可是没有足够的物理卷(硬盘),所以引出了改文.本文共分为上下两部分.这是第一部分. 文件夹 加入磁盘 做LVM 加入硬盘 ...

  3. Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止)

    Windows 共享无线上网 无法启动ICS服务解决方法(WIN7 ICS服务启动后停止) ICS 即Internet Connection Sharing,internet连接共享,可以使局域网上其 ...

  4. 【河南省多校脸萌第六场 E】LLM找对象

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 把n个时间离散化一下. 对于不是相邻的点,在两者之间再加一个空格就好. 这样最多会有1000个位置. 则定义dp[i][k][j] 表示前i个数 ...

  5. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. 彩票案例-frame,center和bounds属性

    控件的属性: 二.frame.center和bounds属性 " 在iOS中,每一个控件都是继承于UIView的.都会有视图的属性存在,控制这个视图的位置就有Frame和Bounds两个属性 ...

  7. UVA 294 294 - Divisors (数论)

    UVA 294 - Divisors 题目链接 题意:求一个区间内,因子最多的数字. 思路:因为区间保证最多1W个数字,因子能够遍历区间.然后利用事先筛出的素数求出质因子,之后因子个数为全部(质因子的 ...

  8. iOS_03_关键字、标识符、注释

    一.学习语法之前的提醒 1. C语言属于一门高级语言,其实,所有高级语言的基本语法组成部分都一样,只是表现形式不太一样. 2. 就好像亚洲人和非洲人,大家都有人类的结构:两只手.两只脚.一个头,只是他 ...

  9. WIN7/8/10下批处理提权工具

    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 自动添加批处 ...

  10. 关于python的深浅拷贝、赋值

    https://blog.csdn.net/weixin_39750084/article/details/81435454