vuex + axios-- to get the data to store in the memory

Disclaimer: This blog is used as a note, the contents of the original but also learn from other bloggers, open - learning - learn 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)
			})
		}
	}
})

 

Guess you like

Origin blog.csdn.net/chushoufengli/article/details/89555911