Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Vue中如何使用fetch()获取本地.json文件

Vue组件中写的fetch()方法,获取不到数据。

运行之后显示:

我应该怎么写才能正确的读取到本地的.json文件?

天蓬老师天蓬老师2748 Tage vor2065

Antworte allen(5)Ich werde antworten

  • PHP中文网

    PHP中文网2017-04-11 13:30:29

    一般本地json文件都放在static里面

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-04-11 13:30:29

    读取.json文件,直接import就可以。

    import data from '../data.json'
    
    console.log(data)

    Antwort
    0
  • 阿神

    阿神2017-04-11 13:30:29

    我使用的方法是使用webpack的json-loader方法,再通过import引入

    //data.json

    {
      "greetText": "Hi there and greetings from JSON!"
    }

    //JS

    import config from 'data.json';
    console.log(config.greetText);
    

    Antwort
    0
  • 怪我咯

    怪我咯2017-04-11 13:30:29

    这个得看下data.json的位置吧,控制台的报错是404,没有找到这个json文件

    Antwort
    0
  • 巴扎黑

    巴扎黑2017-04-11 13:30:29

    用vuex引入,没必要使用fetch

    Antwort
    0
  • StornierenAntwort