现在我使用 onMount 异步函数来访问
const dataAPI = './jsfwperf.json';
let data = [];
onMount(async () => {
const res = await fetch(dataAPI)
.then(res => res.json())
.then(data => {
console.log(data)
})
.catch(err => console.error(err));
});
但在终端它显示文件丢失
bundles src/main.js → public\build\bundle.js...
[!] Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
文件位置与我使用的 app.svelte 相同。
如何通过 svelte 正确访问本地 json 文件?
最佳答案
在命令行中:
cd yourproject
npm install @rollup/plugin-json --save-dev
编辑
rollup.config.js
文件:// add at the beginning of the file :
import json from '@rollup/plugin-json'
//...
// under the plugin line :
plugins: [
json({
compact: true
}),
//...
在您的 slim 组件中:
<script>
import * as myjson from './test.json'
</script>
<p>{myjson.mykey}</p>
关于javascript - 如何通过 svelte 访问本地 json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779816/