【问题标题】:can't fetch local json file in electron-vue无法在 electron-vue 中获取本地 json 文件
【发布时间】:2019-11-19 13:48:52
【问题描述】:

我尝试在我的项目中获取本地 json 文件。 尝试了以下方法:

import axios from 'axios';
import userDataJson from './../data/userData.json';

export const userDataControllerMixin = {
  data() {
    return {
      users: [],
    };
  },
  mounted() {
    this.getUsers();
  },
  methods: {
    getUsers() {
      // TODO: load userObj.json initial to prevent reset the userData.json all the time
      fetch(userDataJson)
        .then((response) => {
          console.log(response);
        }).catch((err) => {
          console.log(err);
        });
    };

我之前也用 axios 试过,但都导致这个错误消息:

GET http://localhost:9080/[object%20Object] 404 (Not Found)

我做错了什么?看起来是配置问题。

【问题讨论】:

    标签: json vue.js electron axios


    【解决方案1】:

    您不再需要调用 fetch 方法,因为您正在从文件中导入 json 数据。你可以做的是,既然你已经在导入 json 数据,那么当组件被挂载时,只需将 json 数据分配给用户数组:

    data() {
        return {
          users: [],
        };
      },
      mounted() {
        this.users = JSON.parse(userDataJson)
      },
    ....
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 1970-01-01
      • 2021-12-03
      • 2019-07-14
      • 2020-03-28
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多