再次关于事件删除项的重复

时间:2019-06-06 07:38:03

标签: vuejs2 event-handling

我在这里I have event duplication after action was moved in store object问 关于事件的重复和建议的决定似乎很好,但是当我回到这个问题之后,我也遇到了同样的问题 成功消息重复项。 在我的容器vue文件MainApp.vue中,设置了事件处理程序:

ItemSale

我使用click.prevent呼叫确认对话框:

<template>
   <body class="account-body">
   <v-dialog/>

   <MainHeader></MainHeader>
   <div class="content p-0 m-0" style="width: 100% !important; margin: auto !important;">
      <notifications group="wiznext_notification"/>
      <router-view></router-view>
   </div>
   </body>

</template>

<script>

    import {bus} from '../../app';
    import MainHeader from './MainHeader.vue';
    import { deleteFromUserListsKey, runDeleteFromUserLists } from "../../helpers/commonFuncs";

    export default {
        name: 'main-app',
        components: {MainHeader},
        created() {
            // console.log('resources/js/components/Horizontal/MainApp.vue Component created.')
        },
        mounted() {
            console.log('resources/js/components/Horizontal/MainApp.vue Component MOUNTED.')
            bus.$on('dialog_confirmed', (paramsArray) => {
                // alert( "resources/js/components/Horizontal/MainApp.vue dialog_confirmed  paramsArray::"+var_dump(paramsArray) )
                if (paramsArray.key == this.deleteFromUserListsKey(paramsArray.user_list_id)) {
                    this.runDeleteFromUserLists(paramsArray.user_list_id, paramsArray.user_id, paramsArray.index);
                }
            })
        },

        methods: {
            deleteFromUserListsKey, runDeleteFromUserLists
        }

    }

</script>

我的混合资源/js/appMixin.js中的方法被触发,并且如果用户选择确认,则会发出dialog_confirmed事件:

<div class="card-footer">
   <div class="float-right">
      <i :class="'mr-2 '+getHeaderIcon('delete')" @click.prevent="confirmDeleteUserList( nextUserList.id, currentLoggedUser.id,
      nextUserList.title, index );" title="Delete"></i>
   </div>

在我的商店资源/js/store.js中,OI定义了用于删除和触发事件的方法:

confirmMsg: function (question, paramsArray, title, bus) {
    this.$modal.show('dialog', {
        title: title,
        text: question,
        buttons: [
            {
                title: 'Yes',
                default: true,    // Will be triggered by default if 'Enter' pressed.
                handler: () => {
                    bus.$emit('dialog_confirmed', paramsArray);
                    this.$modal.hide('dialog')
                }
            },
            {
                title: '',       // Button title
                handler: () => {
                } // Button click handler
            },
            {
                title: 'Cancel'
            }
        ]
    })
},

在list.vue文件中,要从其中删除项目,我定义弹出消息:

userListDelete(context, paramsArray ) {
    axios({
        method: ( 'delete' ),
        url: this.getters.apiUrl + '/personal/user-lists/' + paramsArray.user_list_id,
    }).then((response) => {
        let L = this.getters.userLists.length
        for (var I = 0; I < L; I++) {
            if (response.data.id == this.getters.userLists[I].id) {
                this.getters.userLists.splice(this.getters.userLists.indexOf(this.getters.userLists[I]), 1)
                context.commit('refreshUserLists', this.getters.userLists);
                break;
            }
        }

        bus.$emit( 'onUserListDeleteSuccess', response );
    }).catch((error) => {
        bus.$emit('onUserListDeleteFailure', error);
    });

}, // userListDelete(context, paramsArray ) { 

我在所有页面的安装事件中显示消息,并且看到MainApp.vue的安装事件仅在打开其他页面时触发一次 请参阅控制台中的相关消息。 谷歌搜索我发现了bus。$ off方法,但它无济于事,无论如何我有几条重复的消息,尤其是当我在不同页面之间时。

如何解决这些重复的消息?

谢谢!

1 个答案:

答案 0 :(得分:0)

我在以下位置找到了决定: Stop receiving events from destroyed child component

因此,创建/删除

之类的事件
created() {
    ...
    bus.$on('onUserListDeleteSuccess', (response) => {
        this.is_page_updating = false
        this.showPopupMessage("User lists", 'User\'s list was successfully deleted!', 'success');
    })

}, //  created() {

beforeDestroy(){
    bus.$off( 'onUserListDeleteSuccess' )
},

为我工作正常!