【问题标题】:In sveltekit , how to pass props in slot using __layout.svelte File?在 sveltekit 中,如何使用 __layout.svelte 文件在插槽中传递道具?
【发布时间】:2021-09-28 13:12:51
【问题描述】:

在 __layout 文件中,我想传递一个变量让我们说 btnclicked="HR",我想在 Slot 中呈现的组件中收集相同的变量?请问怎么做?

【问题讨论】:

    标签: svelte sveltekit


    【解决方案1】:

    如果你想传递反应性的数据,那么我认为这样做的方法是使用带有 Context 的 Svelte 存储。

    // in __layout.svelte
      <script>
        import { setContext } from 'svelte';
        import { writable } from 'svelte/store';
    
        const btnClicked = writable('');
    
        setContext('btnClicked', btnClicked);
    
        //...some code that sets the btnClicked store value
      </script>
    
      <slot />
    
    // in child.svelte
      <script>
        import { getContext } from 'svelte';
    
        const btnClicked = getContext('btnClicked');
    
        // use $btnClicked in this component to access the value
      </script>
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多