Vue组件中写的fetch()方法,获取不到数据。
运行之后显示:
我应该怎么写才能正确的读取到本地的.json文件?
用vuex引入,没必要使用fetch
这个得看下data.json的位置吧,控制台的报错是404,没有找到这个json文件
我使用的方法是使用webpack的json-loader方法,再通过import引入
//data.json
{ "greetText": "Hi there and greetings from JSON!" }
//JS
import config from 'data.json'; console.log(config.greetText);
读取.json文件,直接import
就可以。
import data from '../data.json'
console.log(data)
一般本地json文件都放在static里面