私のコードは次のようである:vue.js 2で応答ajaxを取得する方法は?
<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>
スクリプトが実行された場合、それは、それはvuexストア
上のアクションを呼び出しますcheckFavoriteStore方法により
最初checkFavoriteStoreメソッドを呼び出します。そして、結果がします応答を返す
どのように応答を得るには?
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
}
と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に関連するコードを教えてください。 –
ここで私の答えを見てくださいhttps://laracasts.com/discuss/channels/vue/how-to-get-response-ajax-on-the-vuejs-2 –
@Belmin Bedak、This:this。this $ store .dispatch( 'checkFavoriteStore'、ペイロード)。それはvuexストアのアクションを呼び出します –