admin管理员组文章数量:1327945
I stuck a bit with a problem:
I have a svelte ponent, which works with a number of props:
<script>
export let foo;
export let bar;
</script>
But also, I'd like to pass some props to my HTML element directly. So my solution is:
<script>
export let foo;
export let bar;
const {
foo,
bar,
...other
} = $$props;
</script>
<button {...other}>
Some action
</button>
This one has a huge problem: When I change some props like "class", the ponent wouldn't be updated.
<MyComponent {foo} {bar} class={condition ? 'one' : 'two'} />
What is a better way to solve this case? I mean, I have to support different props, not only "class" one. How could I pass the rest of the props to an HTML-element
I stuck a bit with a problem:
I have a svelte ponent, which works with a number of props:
<script>
export let foo;
export let bar;
</script>
But also, I'd like to pass some props to my HTML element directly. So my solution is:
<script>
export let foo;
export let bar;
const {
foo,
bar,
...other
} = $$props;
</script>
<button {...other}>
Some action
</button>
This one has a huge problem: When I change some props like "class", the ponent wouldn't be updated.
<MyComponent {foo} {bar} class={condition ? 'one' : 'two'} />
What is a better way to solve this case? I mean, I have to support different props, not only "class" one. How could I pass the rest of the props to an HTML-element
Share Improve this question edited Jun 30, 2020 at 12:08 Nik asked Jun 30, 2020 at 11:42 NikNik 2,2721 gold badge18 silver badges21 bronze badges1 Answer
Reset to default 6Well... Passing class this way does work for me. See this REPL.
App.svelte
<script>
import MyComponent from './MyComponent.svelte'
let checked
</script>
<label>
<input type=checkbox bind:checked />
Blue?
</label>
<MyComponent class={checked ? 'blue' : 'red'} />
MyComponent.svelte
<script>
export let foo
export let bar
</script>
<pre>foo={foo} bar={bar}</pre>
<button {...$$restProps}>
Some button
</button>
<style>
:global(.red) {
color: red;
}
:global(.blue) {
color: blue;
}
</style>
A couple notes...
Svelte has quite recently introduced $$restProps
(docs -- end of the section), which might be better suited that filtering props manually.
This block of code in your example is not reactive:
const {
foo,
bar,
...other
} = $$props;
It is only executed once when the ponent is created. It is not updated when props change, which might explain why your subsequent change of props (class={condition ? 'one' : 'two'}
) is not reflected.
If you need to do something like this (say because you need more control than what $$restProps
offers out of the box), you need to put this in a reactive block ($:
).
For example:
$: ({ foo, bar, ...other } = $$props)
The above JS syntax is equivalent to the following, more verbose, alternative:
let foo
let bar
$: {
foo = $$props.foo
bar = $$props.bar
}
本文标签: javascriptHow to pass the rest of the props to an HTML element using svelteStack Overflow
版权声明:本文标题:javascript - How to pass the rest of the props to an HTML element using svelte? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742224260a2435779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论