无法在 vuex 商店中访问 axios
Can't access axios in vuex Store
这是我的 store.js 代码:
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios/dist/axios';
import VueAxios from 'vue-axios'
const api = axios.create({
baseURL: 'mybase.com',
timeout: 2000,
});
Vue.use(VueAxios, api)
Vue.use(Vuex);
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
name: null,
YOB: null,
gender: null,
city: null,
daily_goal: null,
balance: null,
today_video_count: 17,
video_credit: 5,
},
mutations: {
updateDailyGoal(state, value) {
state.daily_goal = value;
},
addVideoCredit(state) {
state.balance += state.video_credit;
},
reduceDonation(state, amount) {
state.balance -= amount;
},
setVideoCount(state, count) {
state.today_video_count = count;
},
initialize(state, profile) {
state.name = profile.name;
state.YOB = profile.YOB;
state.gender = profile.gender;
state.city = profile.city;
state.daily_goal = profile.daily_goal;
state.balance = profile.balance;
},
},
actions: {
submitVideoView({commit}, payload) {
this.$http.post('/vid');
commit('addVideoCredit');
},
setDailyGoal({commit}, value) {
this.$http.post('/account/daily-goal', {daily_goal: value});
commit('updateDailyGoal', value);
},
init({commit}) {
this.$http.get('/account/profile').then(response => {
commit('initialize', response);
});
this.$http.get('/account/vid-view').then(response => {
commit('setVideoCount', response);
});
},
},
created() {
dispatch('init');
},
});
但它给我的错误是 undefined doesn't have post 属性 这意味着 this.$http 没有定义。我怎样才能解决这个问题?
这也是我的 main.js:
import Vue from "nativescript-vue";
import App from "./components/App";
import store from "./store";
new Vue({
store,
render: (h) => h("frame", [h(App)]),
}).$start();
如有任何关于如何改进此代码的评论,我们将不胜感激。我真的环顾四周但找不到任何好的文档可以告诉我如何正确设置它。请注意,我只希望在应用程序的每个用户 session/launching 开始时分派一次 init 操作。
我认为您不需要为简单的 HTTP 请求使用 Axios 包装器。
您可以做的是在文件中导入 Axios 模块,然后直接通过 Axios 模块发出请求。
如果需要,您可以将其包装为数据适配器。
import axios from 'axios';
const baseUrl = process.env.BASE_URL || '/';
export default {
get(url, options) {
return axios.get(`${baseUrl}${url}`, options)
.then(response => response.data);
},
post(url, body, options) {
return axios.post(`${baseUrl}${url}`, body, options)
.then(response => response.data);
},
update(url, body, options) {
return axios.put(`${baseUrl}${url}`, body, options)
.then(response => response.data);
},
delete(url, options) {
return axios.delete(`${baseUrl}${url}`, options);
},
}
将该文件命名为 EP/index.js 例如
然后在你的 vuex 中,你可以在下面导入该文件并使用这些功能,就是这样。
这是我的 store.js 代码:
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios/dist/axios';
import VueAxios from 'vue-axios'
const api = axios.create({
baseURL: 'mybase.com',
timeout: 2000,
});
Vue.use(VueAxios, api)
Vue.use(Vuex);
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
name: null,
YOB: null,
gender: null,
city: null,
daily_goal: null,
balance: null,
today_video_count: 17,
video_credit: 5,
},
mutations: {
updateDailyGoal(state, value) {
state.daily_goal = value;
},
addVideoCredit(state) {
state.balance += state.video_credit;
},
reduceDonation(state, amount) {
state.balance -= amount;
},
setVideoCount(state, count) {
state.today_video_count = count;
},
initialize(state, profile) {
state.name = profile.name;
state.YOB = profile.YOB;
state.gender = profile.gender;
state.city = profile.city;
state.daily_goal = profile.daily_goal;
state.balance = profile.balance;
},
},
actions: {
submitVideoView({commit}, payload) {
this.$http.post('/vid');
commit('addVideoCredit');
},
setDailyGoal({commit}, value) {
this.$http.post('/account/daily-goal', {daily_goal: value});
commit('updateDailyGoal', value);
},
init({commit}) {
this.$http.get('/account/profile').then(response => {
commit('initialize', response);
});
this.$http.get('/account/vid-view').then(response => {
commit('setVideoCount', response);
});
},
},
created() {
dispatch('init');
},
});
但它给我的错误是 undefined doesn't have post 属性 这意味着 this.$http 没有定义。我怎样才能解决这个问题? 这也是我的 main.js:
import Vue from "nativescript-vue";
import App from "./components/App";
import store from "./store";
new Vue({
store,
render: (h) => h("frame", [h(App)]),
}).$start();
如有任何关于如何改进此代码的评论,我们将不胜感激。我真的环顾四周但找不到任何好的文档可以告诉我如何正确设置它。请注意,我只希望在应用程序的每个用户 session/launching 开始时分派一次 init 操作。
我认为您不需要为简单的 HTTP 请求使用 Axios 包装器。 您可以做的是在文件中导入 Axios 模块,然后直接通过 Axios 模块发出请求。 如果需要,您可以将其包装为数据适配器。
import axios from 'axios';
const baseUrl = process.env.BASE_URL || '/';
export default {
get(url, options) {
return axios.get(`${baseUrl}${url}`, options)
.then(response => response.data);
},
post(url, body, options) {
return axios.post(`${baseUrl}${url}`, body, options)
.then(response => response.data);
},
update(url, body, options) {
return axios.put(`${baseUrl}${url}`, body, options)
.then(response => response.data);
},
delete(url, options) {
return axios.delete(`${baseUrl}${url}`, options);
},
}
将该文件命名为 EP/index.js 例如 然后在你的 vuex 中,你可以在下面导入该文件并使用这些功能,就是这样。