How do you load a JSON in a Stackblitz project?

纵然是瞬间 提交于 2019-12-04 12:45:42

XHR or fetch is used to get data from remote server. In modules you can simply use import data from './data.json'

Like here: https://stackblitz.com/edit/svelte-wtvhav

mohit kaushik

There is a solution if you are just practicing, make a folder named public at the root level of your project and move the resource you want to access using ajax in it. e.g resource: ./public/data.json you can access it by:

let request = new Request("/data.json");
// **do not** mention url as/public/data.json
fetch(request)
.then((data)=>{
console.log(data);
// your code
});

It will work as by default webpack provides access to folder named public for keeping static content.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!