简体   繁体   English

nuxtJS与Axios和VueX的简单使用

[英]A simple use of nuxtJS with Axios and VueX

I'm discovering VueJS and I have troubles querying an API. 我发现VueJS,但在查询API时遇到了麻烦。 There are (too) many resources, and it looks like there is a different approach for each tutorial I meet. 资源太多了,而且我遇到的每个教程似乎都有不同的方法。 Now I'm getting lost... 现在我迷路了...

The project is a little 3 pages showcase. 该项目只有3页的展示空间。 Content is provided by an API, one endpoint for each language. 内容由API提供,每种语言都有一个端点。 I would like to use VueX to store the data, and to update it according to a language switch interaction. 我想使用VueX来存储数据,并根据语言切换交互来更新数据。

Following the documentation, the "classical" approach is deprecated, and I use the "modules" approach. 遵循文档之后,不赞成使用“经典”方法,而我使用“模块”方法。 Still, I suppose that it's ok to centralize data at the store root as only one API call is required: 不过,我认为将数据集中在商店根目录中是可以的,因为只需要一个API调用即可:

/store/index.js

import axios from "axios";

// STATE - Initial values
export const state = () => ({
  content: {}
});

// ACTIONS - Asynchronous operations
export const actions = () => ({
    async nuxtServerInit({ commit }) {
    // I will introduce the language variable later here
    const response = await this.$axios.$get('https://APIURL.com/fr');
    const content = response.data;
    commit("setContent", content);
  }
});

// MUTATIONS - Updates the state
export const mutations = {
  setContent(state, content) {
    state.content = content;
  }
};

At this point, I expect the content to be made available to the different pages or components. 在这一点上,我希望内容可以提供给不同的页面或组件。

/components/A_component.vue and/or /pages/index.vue /components/A_component.vue和/或/pages/index.vue

...
  {{ content }}
...

// LOADS the store
import { mapState } from "vuex";

// COMPUTES the values retrieval
export default {
  computed: mapState(["content"])
};

However, nothing is displayed. 但是,什么也不显示。 Indeed, the content object is not updated and remains empty. 实际上, content对象不会更新,而是保持为空。

Example for nuxtServerInit nuxtServerInit的示例

// store/settings.js
export const mutations = {
  GET_PRICE_VIEW_FROM_SERVER(ctx, payload) {
    ctx.priceView = payload;
  }
}
// store/index.js
import { getPriceView } from "../utils/getSettings";

export const actions = {
  nuxtServerInit({ commit }, { req }) {
    commit("setting/GET_PRICE_VIEW_FROM_SERVER", getPriceView(req));
  }
};

Using nuxtServerInit usually only for https://nuxtjs.org/api/context#the-context I assume your params.lng should become from the context 通常仅对https://nuxtjs.org/api/context#the-context使用nuxtServerInit ,我假设您的params.lng应该来自上下文

Ok, I finally found where the bug was coming from. 好的,我终于找到了错误的来源。

actions should be exported as an object, not a function: actions应作为对象而不是函数导出:

export const actions = {
    async nuxtServerInit({ commit }) {
    // I will introduce the language variable later here
    const response = await this.$axios.$get('https://APIURL.com/fr');
    const content = response.data;
    commit("setContent", content);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM