小编Nes*_*8x8的帖子

如何使用 sveltekit 提供纯 json 文件?

我尝试在我的端点 routes/users.json.ts 中做这样的事情:

import * as api from '$lib/api'

export async function get({ query, locals }) {

  const response = await this.fetch('static/data/customers.json')

  return {
    status: 200,
    body: {
      data: response
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的静态文件夹位于路由文件夹中。

我收到此错误:

...
TypeError [ERR_INVALID_URL]: Invalid URL: /static/data/customers.json
    at onParseError (internal/url.js:259:9)
    at new URL (internal/url.js:335:5)
    at new Request (file:///home/nkostic/code/example/node_modules/@sveltejs/kit/dist/install-fetch.js:1239:16)
...
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

重要的是它必须是静态 json 文件。

javascript svelte sveltekit

2
推荐指数
1
解决办法
652
查看次数

标签 统计

javascript ×1

svelte ×1

sveltekit ×1