简体   繁体   English

在 svelte 应用程序中的 +page.svelte 上没有加载道具

[英]Prop not loading on +page.svelte in svelte app

Hi I am trying to fetch some data from an API (FastAPI running on same localhost on a different port)嗨,我正在尝试从 API 获取一些数据(FastAPI 在不同端口上的同一本地主机上运行)

here is the +page.js code这是 +page.js 代码

/** @type {import('./$types').PageLoad} */

export async function load({ fetch }) {
    const res = await fetch('http://127.0.0.1:8000');
    let message = await res.json()
    message = message.message
    if (res.ok) {
        console.log(message)
        return {
            message
        }
    }

    throw error("No data Received")
}

and here is +page.svelte:这是+page.svelte:

<script>
    /** @type {import('./$types').PageData} */
    export let message;
</script>
<main>
    <div class="container">
        <h1>Hello World {message}</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae provident, labore qui modi minima voluptate perferendis est beatae non, molestias a, esse facilis deleniti error voluptatum iure harum magni debitis placeat ad! Amet numquam ratione iste?</p>
    </div>
</main>

<style>
    .container {
        width: 80%;
        margin: 0 auto;
        text-align: justify;
    }
    h1 {
        text-align: center;
    }
</style>

The console.log(message) in +page.js is logging the response but the {message} variable in +page.svelte component is undefined. +page.js 中的 console.log(message) 正在记录响应,但 +page.svelte 组件中的 {message} 变量未定义。 what am i doing wrong.我究竟做错了什么。

PS: I am following the sveltekit docs from the official website. PS:我正在关注官方网站上的 sveltekit 文档。

All the props passed down from the various files (page.js, layout.js, ...) are now available in one big data prop.从各种文件(page.js、layout.js、...)传下来的所有道具现在都可以在一个大data道具中使用。

<script>
 export let data;
 // assign it to a variable yourself
 $: message = data.message;
</script>

<!-- or use it directly from data -->
<h1>Hello {data.message}</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM