rest - 使用 NODE.js REST 后端所需的 VUEX
问题描述
我对前端/后端架构不是很有经验,但我使用 NODE.js 创建了一个简单的 REST 后端,并希望构建一个基于 Vue.js 和 Framework7 的前端。
那么你推荐在那里使用 VUEX 吗?或者您如何处理会话或发送到后端的不同请求?
非常感谢!
解决方案
您不必使用 Vuex,但我建议使用 Vuex。这是一个使用 Vuex 和 rest api 的示例。
在商店/actions.js
import {
fetchSomething,
} from '../api/index.js';
export const actions = {
getSomething({ commit }) {
fetchSomething().then((something) => {
commit('UPATED_SOMETHING', something);
});
},
}
在 api/index.js
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);
})
})
}
在 store/mutations.js 中
export const mutations = {
UPATED_SOMETHING(state, data) {
state.something = data;
},
}
在 store/index.js 中
import { getters } from './getters'
import { actions } from './actions'
import { mutations } from './mutations'
// initial state
const state = {
something: null,
}
export default {
state,
getters,
actions,
mutations,
}
在 store/getters.js 中
export const getters = {
getSomething: state => {
return state.something;
},
}
推荐阅读
- mongodb - MongoDB聚合:如果null或不存在,则保留先前文档的值
- json - 如何在颤动中更改对地图的响应类型
- python - Python - 将数据保存在已经存在的大 xlsx 文件中的最快方法
- linux - 通过 ssh 跳转主机和目标主机的不同公钥
- r - 如何建立模型来预测缺失值。?
- java - 如何在按钮 ActionListener 中到达外部 int?
- vue.js - 未捕获的 TypeError:Bootstrap 的 JavaScript 需要 jQuery。在使用 vujs2 时,必须在 Bootstrap 的 JavaScript 之前包含 jQuery
- reporting-services - SSRS 中的级联问题
- c++ - 包含在 2 个 gtest 文件中的静态内联关联集合引发读取访问冲突
- google-apps-script - 基于给定参考格式化单元格的更有效方法