简体   繁体   English

Svelte:使组件对变量有反应(重新渲染)

[英]Svelte: make component reactive to variable (rerender)

I want to rerender "Body" (my svelte component) whenever "view.current" changes so it renders the corresponding.svelte view/component:我想在“view.current”发生变化时重新渲染“Body”(我的 svelte 组件),以便它呈现相应的.svelte 视图/组件:

App.svelte App.svelte

    <script>
    import Header from "./components/Header.svelte";
    import Footer from "./components/Footer.svelte";
    import Body from "./components/Body.svelte";

    import Login from "./views/Login.svelte";
    import Dashboard from "./views/Dashboard.svelte";

    import { view } from "./store";
</script>

<Header />
    <Body>
        {#if view.current === view.login}
            <Login />
        {:else if view.current === view.dashboard}
            <Dashboard />
        {/if}
    </Body>
<Footer />

In "Body.svelte" i just have a slot that gets styled在“Body.svelte”中,我只有一个可以设置样式的插槽

Body.svelte身材苗条

    <div class="container">
    <div class="content">
        <slot></slot>
    </div>
</div>

<style>
    .container {
        padding: 1em;
        display: flex;
    }
    .content {
        margin: auto;
    }
</style>

In Login.svelte (and other svelte components) i want to change "view.current":在 Login.svelte(和其他 svelte 组件)中,我想更改“view.current”:

Login.svelte登录.svelte

<script>
    import { view } from "../store";

    function handleLoginClick() {
        view.current = view.dashboard;
    }
</script>


<button type="button" on:click={handleLoginClick} class="btn btn-primary btn-lg login-btn">Login</button>

<style>
    .login-btn {
        display: block;
        margin: auto;
    }
</style>

store.js商店.js

    const user = {
    username: "",
    fullname: "",
    role: null,
    isLoggedIn: false
};

const view = {
    login: 1,
    dashboard: 2,
    current: 1
};

export {
    user,
    view
}

The value of "view.current" changes as expected, however "Body" does not update/rerender. “view.current”的值按预期更改,但“Body”不会更新/重新呈现。 So it always shows the login.svelte no matter to what "view.current" has been set.所以它总是显示 login.svelte 无论设置了什么“view.current”。 Is there a quick and easy way to make "Body" reactive to "view.current" so that it rerenders so that the if/else-block in "App.svelte" get's reevaluated?有没有一种快速简便的方法可以使“Body”对“view.current”做出反应,以便它重新呈现,以便重新评估“App.svelte”中的 if/else-block?

Importing a regular variable like that in a component creates a local copy of said variable.在组件中导入像这样的常规变量会创建该变量的本地副本。 The view you are referring to in the Login is not shared with the one in App, so the changes are not reflected there.您在登录中引用的view未与 App 中的视图共享,因此更改不会反映在那里。

The "Svelte-way" of sharing state like that accross components is to use a store .像这样跨组件共享 state 的“Svelte-way”是使用store

In your setup that would mean you define view as a store first:在您的设置中,这意味着您首先将视图定义为商店:

import { writable } from 'svelte/store'

const view = writable({
    login: 1,
    dashboard: 2,
    current: 1
});

In the components themselves you have to prefix the store with $在组件本身中,您必须在商店前加上$

<script>
    function handleLoginClick() {
        $view.current = $view.dashboard;
    }
</script>
{#if $view.current === $view.login}
    <Login />
{:else if $view.current === $view.dashboard}
    <Dashboard />
{/if}

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

相关问题 如何使 if 语句重新渲染组件苗条 - How to make if statement rerender component svelte 如何在 Svelte 中使全局变量反应? - How to make global variable reactive in Svelte? Svelte - 如何使数据不反应? - Svelte - how to make data not reactive? 苗条组件不使用父组件的数据重新渲染子组件中的条件 - svelte component not rerender conditionals in child component with data of parent component 卸载组件时,svelte 是否具有内置保护以防止在异步操作中更新反应变量 - Does svelte have built-in protection against updating a reactive variable in async operation when the component is unmounted 使用全局脚本时 Svelte 组件不响应 - Svelte Component Not Reactive When Using Global Script 使用 JavaScript 创建的 Svelte 组件未收到响应式更新 - Svelte component created with JavaScript not receiving reactive updates 不知道如何让这个组件重新渲染 - Not sure how to make this component rerender 如何在苗条的商店中引用反应式 $ 变量? - how to reference a reactive $ variable inside a svelte store? 子组件不会重新渲染,但父组件会重新渲染。 如何让子组件重新渲染? - Child component doesn't rerender but parent component does rerender. How to make child component rerender?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM