iamshaunjp / iamshaunjp/sveltekit-tutorial

[lesson 10] page.params.id

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
96
Forks
41
PR merge metrics
No merged PRs in 30d

Description

when i typed in the code that you showed in the video[lesson 10]:
```javascript

export async function load({ page, fetch }) {
const id = page.params.id
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)
const guide = await res.json()
if (res.ok) {
return {
props: {
guide
}
}
}
return {
status: res.status,
error: new Error('Could not fetch that guide')
}
}

```
I got a error:
```shell
`page` in `load` functions has been replaced by `url` and `params`
Error: `page` in `load` functions has been replaced by `url` and `params`
```
so I use this code to check the arguments:
```javascript
export async function load({ fetch}) {
console.dir(arguments);
}
```
and finally I know how to get the 'id' by using following code:
```javascript

export async function load({ fetch, params }) {
console.dir(arguments); //遇到报错的时候可以用console把参数全部打印出来看看
const id = params.id;
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`);
const guide = await res.json();

if (res.ok) {
return {
props: {
guide,
},
};
}

return {
status: res.status,
error: new Error("Could not fetch the guide"),
};
}

```
Hopefully this can help others with the same bug.(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.