admin管理员组文章数量:1334122
I want to build a custom element using Svelte.
Thus in rollup.config.js
I set customElement: true
, and then I have to use the to refer to my child ponents.
But I found that in this way, the bind
will not work. Here is the code example
HelloWorld.svelte (child)
<script>
import Hello from './ponents/Hello'
import World from './ponents/World'
export let value;
</script>
<svelte:options tag={'x-app-helloworld'}/>
<input type="text" bind:value={value} >
<input>
<x-app-hello />
<x-app-world />
App.svslte(parent) part of it.
<x-app-helloworld bind:value={value}/>
Then the parent will show an error: 'value' is not a valid binding on <x-app-helloworld> elements.
How could I solve this bind
problem?
I want to build a custom element using Svelte.
Thus in rollup.config.js
I set customElement: true
, and then I have to use the to refer to my child ponents.
But I found that in this way, the bind
will not work. Here is the code example
HelloWorld.svelte (child)
<script>
import Hello from './ponents/Hello'
import World from './ponents/World'
export let value;
</script>
<svelte:options tag={'x-app-helloworld'}/>
<input type="text" bind:value={value} >
<input>
<x-app-hello />
<x-app-world />
App.svslte(parent) part of it.
<x-app-helloworld bind:value={value}/>
Then the parent will show an error: 'value' is not a valid binding on <x-app-helloworld> elements.
How could I solve this bind
problem?
1 Answer
Reset to default 8Bindings work on regular elements because Svelte knows which event corresponds to each binding — for example, it knows that the value
of an <input>
changes when the element fires a change
or input
event.
With custom elements, there's no way to know what event (if any) the parent should be listening for. And there isn't currently a neat way to dispatch events from inside the element. So the best option is to pass in a callback to the custom element, and call it whenever the value changes:
<x-app-helloworld onValueChange="{(x) => value = x}"/>
<script>
export let onValueChange;
export let value;
$: onValueChange(value);
</script>
本文标签: javascriptSvelte bind is not working when customElement true is setStack Overflow
版权声明:本文标题:javascript - Svelte bind is not working when customElement: true is set - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742350516a2458397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论