【发布时间】:2020-03-20 04:22:28
【问题描述】:
根据 Svelte 文档:
动作是在创建元素时调用的函数。他们可以返回一个带有在元素卸载后调用的destroy方法的对象
我想将多个参数传递给一个 Svelte 动作函数,但只有最后一个被识别
<script>
function example(node, arg1, arg2) {
// the node has been mounted in the DOM
console.log(arg1, arg2) // Should display 'a b', but actually only displays 'b undefined'
return {
destroy() {
// the node has been removed from the DOM
}
}
}
</script>
<h1 use:example={'a', 'b'}>Hello World!</div>
是否有任何可行的解决方案避免使用单个对象作为参数?
<script>
function example(node, arg) {
// the node has been mounted in the DOM
console.log(arg) // Returns a object with the arguments
return {
destroy() {
// the node has been removed from the DOM
}
}
}
</script>
<h1 use:example>Hello World!</div>
<!-- Passing parameters -->
<h1 use:example={{
arg1: [50, 75, 100],
arg2: true
}}>Works like a charm!</h1>
【问题讨论】:
标签: javascript svelte