如何加总+;1在每次使用vue.js 2发送通知时的文本范围内?
我的vue组件如下所示:如何加总+;1在每次使用vue.js 2发送通知时的文本范围内?,vue.js,vuejs2,vue-component,vuex,Vue.js,Vuejs2,Vue Component,Vuex,我的vue组件如下所示: <template> ... <span v-if="total > 0" class="badge" id="total">{{ total }}</span> ... </template> <script> import { mapGetters } from 'vuex' export default { mounted() {
<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的Success回调中,但首先必须定义变异:
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未定义
如我所述,您必须导入您的vuex store抱歉,我现在在电话上,可以稍后写入您必须导入整个vuex store对象,并且必须在之前导出它。