1

I'm trying to get the json file without a server.

Vuex store is undefined in NUXT middleware I refer to this link and call it middleware.

json file failed to load and there was no error. I have no idea what the problem is.

nuxt.config.js

 modules: ["@nuxtjs/axios"],
 axios: {
    baseURL: process.env.BASE_URL
 },

middleware/data.js

import axios from "axios";

export default function({ params, store }) {
  return axios.get(require("~/data/character.json")).then(response => {
    store.commit("SET_CHARACTERS", response.data.results);
  });
}

store/index.js

export const mutations = {
    SET_CHARACTERS: (state, payload) => {
      state.character = payload;
    },
}

Restarting dev server is not work.

1 Answer 1

4

You can try importing the JSON file directly in your middleware, without using axios:

import characters from '~/data/character.json';

export default function({ params, store }) {
   store.commit("SET_CHARACTERS", characters`);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.