在NODE.js REST后端使用VUEX是必要的
我对前端/后端架构不是很有经验,但我使用NODE.js创建了一个简单的REST后端,并希望基于Vue.js和Framework7构建一个前端 那么您建议在那里使用VUEX吗?或者,您如何处理发送到后端的会话或不同请求在NODE.js REST后端使用VUEX是必要的,rest,vue.js,frontend,vuex,Rest,Vue.js,Frontend,Vuex,我对前端/后端架构不是很有经验,但我使用NODE.js创建了一个简单的REST后端,并希望基于Vue.js和Framework7构建一个前端 那么您建议在那里使用VUEX吗?或者,您如何处理发送到后端的会话或不同请求 非常感谢 您不必使用Vuex,但我建议您使用Vuex。下面是一个使用Vuex和RESTAPI的示例 店内/actions.js import { fetchSomething, } from '../api/index.js'; export const actions =
非常感谢 您不必使用Vuex,但我建议您使用Vuex。下面是一个使用Vuex和RESTAPI的示例 店内/actions.js
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
export const fetchSomething = () => {
const url = 'Some endpoint';
return new Promise((resolve) => {
axios.get(url).then((res) => {
const data = res.data;
resolve(data);
}).catch((err) => {
console.log(err);
})
})
}
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
export const getters = {
getSomething: state => {
return state.something;
},
}
在api/index.js中
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
export const fetchSomething = () => {
const url = 'Some endpoint';
return new Promise((resolve) => {
axios.get(url).then((res) => {
const data = res.data;
resolve(data);
}).catch((err) => {
console.log(err);
})
})
}
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
export const getters = {
getSomething: state => {
return state.something;
},
}
店内/mutations.js
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
export const fetchSomething = () => {
const url = 'Some endpoint';
return new Promise((resolve) => {
axios.get(url).then((res) => {
const data = res.data;
resolve(data);
}).catch((err) => {
console.log(err);
})
})
}
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
export const getters = {
getSomething: state => {
return state.something;
},
}
店内/index.js
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
export const fetchSomething = () => {
const url = 'Some endpoint';
return new Promise((resolve) => {
axios.get(url).then((res) => {
const data = res.data;
resolve(data);
}).catch((err) => {
console.log(err);
})
})
}
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
export const getters = {
getSomething: state => {
return state.something;
},
}
店内/getters.js
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
export const fetchSomething = () => {
const url = 'Some endpoint';
return new Promise((resolve) => {
axios.get(url).then((res) => {
const data = res.data;
resolve(data);
}).catch((err) => {
console.log(err);
})
})
}
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
export const getters = {
getSomething: state => {
return state.something;
},
}
好的,这将是我可以存储的方式,例如,我在向rest api请求登录后获得的承载令牌?还是这里有不同的方式?我必须阅读官方文件,我想我要理解整个机制。当然,你可以在Vuex商店中存储一个不记名代币。但是,如果刷新页面,Vuex存储将重置。我会将令牌存储在cookie中,但我仍然会使用Vuex来实现这一点。