vuex+axios——将get到的数据存到store中
Home.vue:router_submit () {this.$store.dispatch('getResult')this.$router.push({ path: '/Questions' })}store.js:import Vue from 'vue'import Vuex from 'vuex'import axios from 'axios...
·
Home.vue:
router_submit () {
this.$store.dispatch('getResult')
this.$router.push({ path: '/Questions' })
}
store.js:
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
// 题号
index: 0,
// 题目数据
result: '',
// 用户答案数组
userAnswersArray: [],
// 服务器地址
url: 'http://localhost:3000'
},
mutations: {
// 切换题号
switch_question (state, target) {
state.index = target
},
// 获取题目
getResult (state, result) {
state.result = result
},
// 选择选项
select (state, target) {
state.userAnswersArray[state.index] = target
}
},
actions: {
// 获取题目
getResult (context) {
axios.get(context.state.url + '/api/result').then(response => {
context.commit('getResult', response.data.result)
})
}
}
})
更多推荐
已为社区贡献1条内容
所有评论(0)