我正试图在SvelteKit中建立一个艺术组合,追随SvelteKit tutorial.我有一个名为art/
的页面,它有一个名为[collections]/
的子目录,用于存放各种不同的艺术作品Collection .
我临时从一个名为data.js
的文件加载数据,该文件具有以下 struct :
export const collections = [
{
title: 'Crisis Vision',
id: 'crisis-vision',
content: '<p>crisis vision content</p>'
},
{
title: 'From the Garden',
id: 'from-the-garden',
content: '<p>from the garden content</p>'
},
{
title: 'As Lost Through Collision',
id: 'as-lost',
content: '<p>as lost content</p>'
}
];
[collections]/
中的+page.server.js
文件包含以下内容:
import { error } from '@sveltejs/kit';
import { collections } from '../data.js';
export function load( {params} ) {
const ret = collections.find((c) => c.id === params.id);
return {
ret
};
}
+page.svelte
文件包含以下内容:
<script>
export let data;
console.log(JSON.stringify(data))
</script>
<h1>{data.ret.title}</h1>
<div>{@html data.ret.content}</div>
当我试着运行这个时,我得到TypeError: Cannot read properties of undefined (reading 'title')
.
我相信我已经将范围缩小到params
变量.当我取出+page.server.js
文件中的params.id
,并直接用数据中的一个示例替换它时,比如'as-lost'
,它就会像预期的as shown here那样工作(当然,每个集合都显示相同的内容).当我将console.log(JSON.stringify(data))
添加到页面文件的脚本部分时,它显示参数对象为空.
我到底做错了什么?我试着在每一步都遵循教程,但它仍然不起作用.请帮帮忙.