每次使用 vue.js 2 通知时,如何在文本范围内添加总数 + 1?

How to add the total + 1 in the text span each time a notification using vue.js 2?

我的vue组件是这样的:

<template>
    ...
        <span v-if="total > 0" class="badge" id="total">{{ total }}</span>
    ...
</template>
<script>
    import { mapGetters } from 'vuex'
    export default {
        mounted() {
            this.initialMount()
        },
        computed: {
            ...mapGetters(['total'])
        },
        methods: {
            initialMount() {
                Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
                     const a = $('#total').text()
                     const b= parseInt(a) + 1
                     $('#total').text(b)
                })
            },
        }
    }
</script>

我的模块是这样的:

import { set } from 'vue'
import notification from '../../api/notification'
import * as types from '../mutation-types'

const state = {
    total: 0,
}

const getters = {
    total: state => state.total
}

const actions = {
    getNotificationList ({ commit,state })
    {
        notification.getList(
            data => {
                const notifications = data
                commit(types.GET_NOTIFICATION,{ notifications });
            },
            errors => {
                console.log(errors)
            }
        )
    }
}

const mutations = {
    [types.GET_NOTIFICATION] (state, { notifications }) {
        state.total = notifications.length
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}

============================================= ======================

我要每条通知,通知号加1

我上面的代码有效,但它仍然使用 jquery

我想使用 vue.js

进行更改

我该怎么做?

您必须将操作提交到 Echo 的成功回调中,但首先您必须定义突变:

const mutations = {
    [types.GET_NOTIFICATION] (state, { notifications }) {
        state.total = notifications.length
    },
    inc (state) {
      state.total++
    }
}

然后,您可以提交操作

methods: {
    initialMount() {
        Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
          // Make sure you have imported store
          store.commit('inc')
        })
    },
}