【问题标题】:How to add the total + 1 in the text span each time a notification using vue.js 2?每次使用 vue.js 2 的通知时,如何在文本跨度中添加总 + 1?
【发布时间】:2017-03-29 15:14:01
【问题描述】:

我的 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 改变它

我该怎么做?

【问题讨论】:

  • 错误是什么,看起来是正确的!

标签: vue.js vuejs2 vue-component vuex


【解决方案1】:

您必须在 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')
        })
    },
}

【讨论】:

  • 控制台存在错误:ReferenceError: store is not defined
  • 正如我提到的,你必须导入你的 vuex 商店
  • 对不起,我现在在打电话,稍后可以写
  • 您必须导入整个 vuex 存储对象,并且必须先导出它。
猜你喜欢
  • 2017-08-22
  • 2022-01-06
  • 2014-05-23
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多