两个子组件之间传值,最好使用一个中间桥梁父组件来进行传值。
子组件1—>父组件—>子组件2
本例想实现的功能:把子组件1中的status传递到子组件2
子组件1:
<el-table
:header-cell-style="{ background: '#3A5FA6', color: '#fff', fontWeight: 'bold' }"
:data="tableData"
stripe
height="770px"
:cellStyle="cellStyle"
style="width:100%;"
highlight-current-row
@current-change="currentChange"
>
<el-table-column prop="no" label="序号" width="50px"></el-table-column>
<el-table-column label="设备类型">
<template slot-scope="scope">
<span :style="scope.row.status == '0' ? 'color:yellow' : 'color:green'">{{ scope.row.controlMethod }}</span>
</template>
</el-table-column>
</el-table>
methods: {
currentChange(currentRow) {
this.$emit('openFacilityAssetDetail', currentRow)
}
}
父组件:
<!--引子组件1-->
<div class="strategy-info facilityAsset" v-if="facilityAsset">
<FacilityAsset @openFacilityAssetDetail="openFacilityAssetDetail"> </FacilityAsset>
</div>
<!--引子组件2-->
<div class="strategy-info facilityAssetDetail" v-if="facilityAssetDetail">
<FacilityAssetDetail :status="status" @viewFacilityAsset="viewFacilityAsset" @addFacilityAsset="addFacilityAsset"></FacilityAssetDetail>
</div>
openFacilityAssetDetail(param) {
//console.log(param)
this.status = param.status//从子组件1中引来的参数
this.facilityAssetDetail = true
}
子组件2
<el-button type="primary" size="medium" @click="viewData" v-if="status == '1'">查看工单</el-button>
<el-button type="primary" size="medium" @click="createData" v-else-if="status == '0'">创建工单</el-button>
export default {
props: {
status: {
type: String,
},
},