vue.config.js
module.exports={
devServer:{
proxy:{
"/api":{
target:"http://localhost:3001/"
}
}
}
}
bd.json
"api":[
{
"id":1,
"name":"小黑",
"phone":"155300000000",
"address":"北京市"
},
{
"id":2,
"name":"小花",
"phone":"155300111110",
"address":"广州"
}
]
store>index.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
Vue.use(Vuex)
export default new Vuex.Store({
state: {
list:[],
},
mutations: {
setData(state,payload){
state.list = payload;
}
},
actions: {
async getData({commit}) {
let res = await axios.get("/api");
console.log(res);
commit("setData",res);
}
},
modules: {
}
})
list组件
<template>
<div>
</div>
</template>
<script>
import {mapState,mapActions} from "vuex"
export default {
data(){
return {
}
},
created(){
this.getData();
},
methods:{
...mapActions(["getData"])
},
}
</script>
<style>
</style>
之前也遇到过,通过这个解决的500问题。
https://blog.csdn.net/Carrie_...