基础
<keep-alive>
<component v-bind:is="currentTabComponent"></component>
</keep-alive>
currentTabComponent可以是
- 已注册组件的名字,
- 或 一个组件的选项对象
换句话说is attribute 接受一个组件名字比如ElementUI的elInput
data() {
return {
currentTabComponent: elInput
}
}
或者 组件的选项对象
currentTab: {
name: "Home",
component: {
template: "<div>Home component</div>"
}
},
currentTabComponent: currentTab.component
keep-alive
你每次切换新标签的时候,Vue 都创建了一个新的 currentTabComponent 实例。
而keep-alive可以避免反复重渲染导致的性能问题。我们更希望那些组件实例能够被在它们第一次被创建的时候缓存下来。
非 Prop 的 Attribute
一个非 prop 的 attribute 是指传向一个组件,但是该组件并没有相应 prop 定义的 attribute。
因为显式定义的 prop 适用于向一个子组件传入信息,然而组件库的作者并不总能预见组件会被用于怎样的场景。这也是为什么组件可以接受任意的 attribute,而这些 attribute 会被添加到这个组件的根元素上。
例如,想象一下你通过一个 Bootstrap 插件使用了一个第三方的 组件,这个插件需要在其 上用到一个 data-date-picker attribute。我们可以将这个 attribute 添加到你的组件实例上:
<bootstrap-date-input data-date-picker="activated"></bootstrap-date-input>
然后这个 data-date-picker=“activated” attribute 就会自动添加到 的根元素上。