svelte - 如何读取苗条组件的属性值
问题描述
组件.svelte
<script>
export let hiddenStatus;
</script>
<div class:hidden={hiddenStatus}></div>
App.svelte
<script>
import Component from "./Component.svelte";
</script>
<Component hiddenStatus={true}/>
{Component.hiddenStatus}
当我尝试获取它的属性值时,hiddenStatus
它显示为undefined
. 我该如何解决这个问题?
解决方案
您可以使用bind:this
组件指令以编程方式与组件实例进行交互。
accessors
请注意,除非您使用settrue
或进行编译,否则无法直接从组件实例中读取 props <svelte:options accessors/>
。
例子
<!-- Component.svelte -->
<script>
export let hiddenStatus;
</script>
<div class:hidden={hiddenStatus}></div>
<!-- App.svelte -->
<script>
import { onMount } from "svelte";
import Component from "./Component.svelte";
let component;
onMount(() => {
console.log(component.hiddenStatus);
});
</script>
<Component hiddenStatus={true} bind:this={component}/>
推荐阅读
- r - 如何为 R Shiny 中的可扩展矩阵自动生成顺序列标题?
- java - 为什么同一组中的 kafka-0.9 多个消费者使用相同的消息
- python - 如何将照片上传到 MySQL 并浏览
- google-chrome-extension - Chrome 扩展和第三方 cookie 替代方案
- file - 如何打开带有 .zvX 文件扩展名的文件?
- postgresql - 如果 nickName 不为 null,则按 nickName 排序,否则按 firstName 排序
- api - Facebook API - 发布到页面不起作用
- android - Android AnimatorSet.cancel() 不适用于 Marshmallow 版本的设备
- .net - 有条件地包含基于路径的项目
- r - 带有ggplot的条形图或一组变量