vuex+axios——将get到的数据存到store中

版权声明:此博客用作笔记,内容有原创也有借鉴其他博主,开放-学习-借鉴 https://blog.csdn.net/chushoufengli/article/details/89555911

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)
			})
		}
	}
})

猜你喜欢

转载自blog.csdn.net/chushoufengli/article/details/89555911