Vue.js:删除评论的突变

时间:2018-02-07 14:51:41

标签: javascript vue.js action mutation

我一直在研究删除评论的功能,并且遇到了有关动作变异的问题。 这是我的客户:

delete_post_comment({post_id, comment_id} = {}) {
// DELETE /api/posts/:post_id/comments/:id
return this._delete_request({
  path: document.apiBasicUrl + '/posts/' + post_id + '/comments/' + comment_id,
 });
}

这是我的商店:

import Client from '../client/client';
import ClientAlert from '../client/client_alert';
import S_Helper from '../helpers/store_helper';

const state = {
 comment: {
  id: 0,
  body: '',
  deleted: false,
 },
 comments: [],
};

const actions = {
 deletePostComment({ params }) {
 // DELETE /api/posts/:post_id/comments/:id
 document.client
  .delete_post_comment({ params })
  .then(ca => {
    S_Helper.cmt_data(ca, 'delete_comment', this);
  })
  .catch(error => {
    ClientAlert.std_fail_with_err(error);
  });
 },
};

 delete_comment(context, id) {
 context.comment = comment.map(comment => {
   if (!!comment.id && comment.id === id) {
     comment.deleted = true;
     comment.body = '';
    }
  });
 },
};

export default {
 state,
 actions,
 mutations,
 getters,
};

我不太确定我是否正确地写了突变。到目前为止,当我通过组件内的on-click调用操作时,没有任何事情发生。

1 个答案:

答案 0 :(得分:1)

猜测你正在使用vuex,流程应该是:

enter image description here

根据此流程,在组件模板上

@click="buttonAction(someParams)"

vm实例,方法对象:

buttonAction(someParams) {
    this.$store.dispatch('triggerActionMethod', { 'something_else': someParams })
}

vuex 操作 - 使用逻辑操作,ajax调用ecc。

triggerActionMethod: ({commit}, params) => {
    commit('SOME_TRANSATION_NAME', params)
}

vuex 突变 - 使用突变将更改变为您的状态

'SOME_TRANSATION_NAME' (state, data) { state.SOME_ARG = data }