Мой код выглядит так:Как получить ответ ajax на vue.js 2?
<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 магазине
и результаты вернуть ответ
Как получить ответ?
UPDATE
Мои действия на 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
}
И АНИ, как это:
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? –
взгляните на мой ответ здесь https://laracasts.com/discuss/channels/vue/how-to-get-response-ajax-on-the-vuejs-2 –
@Belmin Bedak, This: this. $ Store .dispatch ('checkFavoriteStore', полезная нагрузка). Он вызовет действие в магазине vuex –