Howto do something when an attribute changes in svelte

喜夏-厌秋 提交于 2019-12-13 03:09:50

问题


Creating a web component using svelte, I need to run some code when an attribute of the component is changed.

What I have come up with is the following pattern.

<script>
    export let test = "default value";

    $: {
        testIsChanged(test);
    }

    function testIsChanged(newValue) {
        console.log(newValue);
    }
</script>

The value of test is {test} 

Is this the way to do it? Or am I missing something?


回答1:


That will indeed work, as you can see in this REPL



来源:https://stackoverflow.com/questions/57976936/howto-do-something-when-an-attribute-changes-in-svelte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!