【发布时间】:2018-08-18 08:13:40
【问题描述】:
import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
import api from "./api_key";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
data: "",
location: "New York"
},
mutations: {
GET_DATA(state, data) {
state.data = data;
}
},
actions: {
getData({ commit }, state) {
axios
.get(
`https://api.openweathermap.org/data/2.5/forecast/daily?q=${
state.location
}&appid=${api}&units=metric&cnt=5`
)
.then(data => {
commit("GET_DATA", data);
})
.catch(function(error) {
console.log(error);
});
}
},
getters: {
data(state) {
return state.data;
}
}
});
我正在开发 Vue Web 应用程序。对于状态管理,我决定使用 vuex。我的“位置”和“api”变量在我的 Axios 请求地址中未定义。
【问题讨论】: