【发布时间】:2017-06-12 10:11:06
【问题描述】:
我的代码是这样的:
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
alert('tost');
event.target.disabled = true
const payload= {id_store: this.idStore}
this.$store.dispatch('addFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
},
checkFavoriteStore(){
const payload= {id_store: this.idStore}
this.$store.dispatch('checkFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
//get response here
}
}
}
</script>
脚本执行时会先调用checkFavoriteStore方法
通过checkFavoriteStore方法调用vuex store的action
结果会返回响应
如何获得响应?
更新
我对 vuex 商店的操作是这样的:
import { set } from 'vue'
import favorite from '../../api/favorite'
import * as types from '../mutation-types'
// actions
const actions = {
checkFavoriteStore ({ dispatch,commit,state },payload)
{
favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
}
}
// mutations
const mutations = {
[types.CHECK_FAVORITE_SUCCESS] (state){
state.addStatus = 'success'
},
[types.CHECK_FAVORITE_FAILURE] (state){
state.addStatus = 'failure'
}
}
export default {
actions,
mutations
}
还有这样的 api:
import Vue from 'vue'
import Resource from 'vue-resource'
Vue.use(Resource)
export default {
// api check favorite exist or not
checkFavorite (favorite, cb, ecb = null ) {
Vue.http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then(
(resp) => cb(resp.data),
(resp) => ecb(resp.data)
);
}
}
【问题讨论】:
-
能否请您向我们展示您与 AJAX 相关的代码?
-
@Belmin Bedak,这:this.$store.dispatch('checkFavoriteStore', payload)。它将在 vuex 存储上调用操作
-
他要求的是商店中的代码,该代码调用并返回此调用的响应 this.$store.dispatch('checkFavoriteStore', payload)
-
正如 laracast 上提到的,我认为它会在 1.5 秒后刷新页面,因此会错过任何响应????
标签: javascript ajax vue.js laravel-5.3 vuejs2