1.创建axios的js
export function fetch(url, query) { return new Promise((resolve, reject) => { axios.get(url, query) .then(response => { // alert('Api--ok'); resolve(response); }) .catch((error) => { console.log(error); reject(error); }); });}
1封装的请求get,post一样改个请求方式就可以了
2后面直接在js文件调用fetch
3:example:
export const getModelTypeData = () => { return fetch(`${config.baseUrl.core}authority/getModelTypeData`);};
在下面store.js调用 getModelTypeData
2:const app = { state: { ModelTypeData: [] }, getters: { ModelTypeData: state => state.ModelTypeData }, mutations: { getModelTypeAllData(state,allDatas){ state.ModelTypeData = allDatas; } }, actions: { getModelTypeAllData({commit}) { getModelTypeData().then(res=>{ console.log(res.bo); const datas = res.bo && res.bo; commit("getModelTypeAllData",datas); }); } }};
3:在要调用 ModelTypeData数据的组件的created里面
created(){ this.$store.dispatch(' getModelTypeAllData '); },
computed: { ...mapGetters([ ' ModelTypeData ' ]) },
后面直接用this. ModelTypeData调用
4:如果在方法里调用,在mounted中用,初始界面获取不到,用watch监听这个方法,如在方法 controlId中调用 this. ModelTypeData,初始师空,那就像下面一样watch
watch:{ModelTypeData :{ handler(){ this.controlId(); }, deep: true }}
网友评论