
我们都知道,vue的组件化是他最强大的核心所在,路由也是特别可爱的一部分,但是路由适合一些大型的组件,看url路径的时候会出现变化,但是有时候我们想要一些小的局部小刷新,这个时候就需要用到它的动态组件了。
Vue 自身保留的
代码如下:slotDome.vue:
<template>
<div>
<slot></slot>
<slot></slot>
<el-radio-group>
<el-radio-button>
<button>页面一</button></el-radio-button>
<el-radio-button><button>页面二</button>
</el-radio-button>
</el-radio-group>
<keep-alive>
<component></component>
</keep-alive>
</div>
</template><style>
el-radio-button
&:hover
cursor pointer
</style><script>
import simple1 from "./simple/simple1.vue";
import simple2 from "./simple/simple2.vue";
export default{
data(){
return {
tabView: "simple1"
}
},
methods: {
toSim(index){
this.tabView = `simple${index}`;
}
},
components: {
simple1,
simple2
}
}
</script>
simple1.vue:
<template> <div> 这是页面一 <input> </div> </template>
simple2.vue:
<template> <div> 这是页面二 <input> </div> </template>
上例中,当 tabView 的值改变,
将静态 Vue 页面(index.vue)改造为动态页面(Custom.vue);稿源仅限 index.vue、其引用的 CSS 与资源路径,禁止参考仓库内其他文件。Invoke when user wants to transform a static Vue page into a dynamic data-driven page with Element UI components.
但这样一来,每次切换组件都会重新渲染,无法保留组件上的数据。这时可以使用 keep-alive 将组件保留在内存中,避免重新渲染
页面效果如下:

相关推荐:2020年前端vue面试题大汇总(附答案)vue教程推荐:2020最新的5个vue.js视频教程精选
更多编程相关知识,请访问:编程入门!!
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










