1.问题
vue官方文档中,使用keep-alive让标签的组件实例,在它们第一次被创建的时候缓存下来。如何实现在没有点击button的时候(比如下图中的旅客身份证信息),组件已经被激活缓存。
2.想要实现的功能
如果一开始就点击table表(通过bus传递当前行数据),再点击旅客身份证信息,可以查看信息
3.问题详述
如果一开始就点击table表(通过bus传递当前行数据),再点击旅客身份证信息,发现数据没有成功传输如下图
如果要查看旅客身份证信息,需要点击旅客身份证信息后,再点击table表,这样才可以显示旅客数据,如下图。
在激活了【旅客身份证信息】这个组件后,点击table表可以顺利在此组件中显示旅客信息,所以在我看来要实现 2 中所提到的功能需要激活所有 import的组件。
下面是我的代码,想向前辈们请教一下,如何激活所有 import的组件
或者说实现这种功能(点击table后可显示)的其他办法
<template>
<div>
<el-button-group>
<el-button v-for="tab in tabs"
:key="tab.componentName"
size="mini"
v-bind:class="['tab-button', { active: currentTab === tab }]"
v-on:click="currentTab = tab">{{ tab.label }}</el-button>
</el-button-group>
<keep-alive>
<component v-bind:is="currentTabComponent"
class="tab"
:passengerBus='this.passengerBus'></component>
</keep-alive>
</div>
</template>
<script>
import CarryOnBags from './components/CarryOnBags'
import CheckedLuggage from './components/CheckedLuggage'
import PassengerCardInfo from './components/PassengerCardInfo'
import PeerPassengers from './components/PeerPassengers'
import StaffInformation from './components/StaffInformation'
import XRayImage from './components/XRayImage'
export default {
components: {
CarryOnBags, CheckedLuggage, PassengerCardInfo, PeerPassengers, StaffInformation, XRayImage
},
props: ['passengerBus'],
data () {
return {
tabs: [
{
componentName: 'peer-passengers',
label: '同行旅客'
},
{
componentName: 'checked-luggage',
label: '交运行李'
},
{
componentName: 'carry-on-bags',
label: '随身行李开包'
},
{
componentName: 'staff-information',
label: '工作人员信息'
},
{
componentName: 'passenger-card-info',
label: '旅客身份证信息'
},
{
componentName: 'x-ray-image',
label: 'X光图片'
}
],
currentTab: {
componentName: 'peer-passengers',
label: '同行旅客'
}
}
},
computed: {
currentTabComponent: function () {
return this.currentTab.componentName
}
}
}
</script>
<style>
</style>
解决问题了,不需要激活其他组件。
不往其他组件里面传bus,直接往各个组件传需要显示的数据就可以了