EventBus uses pits encountered

**Problem:** The parameters passed through eventBus cannot be used through this. in the methods of subcomponents.
** Idea: **Consider the execution order of component methods (vue life cycle execution order)
**Solution: **This.$nextTick outside the component passing parameters

this.$nextTick(() => {
    Bus.$emit('updateOpt', opt, item)
})

Use of eventBus

  1. Create eventBus.js file
  2. Introduce the js file in the brother component that needs to pass the message
  3. pass message bus.$emit("test",param1,param2...)
  4. Receive message bus.$on("test", funciton({ // logic }))

code show as below:

<!-- 父组件 -->
<template>
    <div>
        <a></a>
        <b></b>
    </div>
</template>

<script>
import a from "./ePassList/a.vue";
import b from "./ePassEdit/b.vue";
export default {
    components: {
        a,
        b,
    },
}
</script>

<style>

</style>
//eventBus.js文件
import Vue from 'vue'

export default new Vue()
<!-- 子组件a 传递消息import Bus from "../eventBus.js"; Bus.$emit('updateOpt', opt, item) -->
<template>
    <div>
        <el-table :data="dataList" size="small" v-loading="loading" border :header-cell-style="{
            color: '#495060',
            'background-color': '#f8f8f9',
        }" style="border: 1px solid #dddee1">
            <el-table-column type="index" label="序号" width="90" align="center">
            </el-table-column>
            <el-table-column prop="activity_name" label="活动名称" align="center">
            </el-table-column>
            <el-table-column prop="activity_type" label="活动类型" align="center">
            </el-table-column>
            <el-table-column prop="activity_publisher" label="发布人" align="center">
            </el-table-column>
            <el-table-column prop="insert_time" label="发布时间" align="center">
            </el-table-column>
            <el-table-column prop="update_time" label="更新时间" align="center">
            </el-table-column>
            <el-table-column prop="action" label="操作" align="center" fixed="right">
                <el-row slot-scope="scope">
                    <Button type="primary" size="small" @click="operate('editOpt', scope.row)">编辑</Button>
                    <Button type="primary" size="small" @click="operate('detailOpt', scope.row)">详情</Button>
                    <Button type="primary" size="small" @click="onDelete(scope.row)">删除</Button>
                </el-row>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import Bus from "../eventBus.js";
export default {
    methods: {
        //编辑,详情
        operate(opt, item) {
            console.log(opt, item);
            this.$emit('toEdit')
            this.$nextTick(() => {
                Bus.$emit('updateOpt', opt, item)
            })
        },
    }
};
</script>

<style></style>
<!-- 子组件b 接收消息import Bus from "../eventBus.js"; Bus.$on("updateOpt", () => {}); -->
<template></template>

<script>
import Bus from "../eventBus.js";
export default {
    data() {
        return {
            operate: "",
            rowData: "",
        }
    },
    created() {
        Bus.$on("updateOpt", (opt, item) => {
            console.log(opt, item, 11111);
            this.operate = opt;
            this.rowData = item;
        });
    },
    methods:{
        print(){
            console.log(this.operate,this.rowData)
        }
    }
};
</script>

<style></style>

Knowledge points involved: nextTick()

nextTick() is to delay the call of the callback function after the next DOM update data.
The simple understanding is: when the data is updated, the function will be executed automatically after the DOM is rendered.
Detailed interpretation link: https://juejin.cn/post/6971017407733170184

Guess you like

Origin blog.csdn.net/qq_43907534/article/details/131889480