【发布时间】:2018-09-06 03:09:27
【问题描述】:
我正在使用以下代码进行测试:
https://jsfiddle.net/b2qj69o1/25/
<button
v-for="tab in tabs"
v-bind:key="tab.name"
v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
v-on:click="currentTab = tab"
>{{ tab.name }}</button>
<component
v-bind:is="currentTab.component"
class="tab"
></component>
还有js部分
var tabs = [
{
name: 'Home',
component: function() {
alert(); // test
return new Promise((resolve, reject) => resolve({
template: '<div>Home component</div>'
}))
}
},
{
name: 'Posts',
component: {
template: '<div>Posts component</div>'
}
},
{
name: 'Archive',
component: {
template: '<div>Archive component</div>',
}
}
]
new Vue({
el: '#dynamic-component-demo',
data: {
tabs: tabs,
currentTab: tabs[1]
}
})
我做了 alert() 测试看看 vue 是否会重新渲染组件。我看到无论是否使用<keep-alive> 包装<component>,如果仅在我第一次进入主页选项卡时调用alert()。所以我有两个问题:
1. keep-alive到底是做什么的?因为似乎无论如何该组件只创建一次。
2. vue 是否可以显示/隐藏选项卡,而不是替换单个 DOM 元素?但是在组件显示出来之前还是不要加载它。
【问题讨论】:
标签: javascript vue.js components keep-alive