美文网首页
Getting started with Svelte - Ac

Getting started with Svelte - Ac

作者: 游文影月志 | 来源:发表于2023-12-05 21:47 被阅读0次

Actions are essentially element-level lifecycle functions. They're useful for things like:

  • interfacing with third-party libraries
  • lazy-loaded images
  • tooltips
  • adding custom event handlers

Actions are functions that are called when an element is created. They can return an object with a destroy method that is called after the element is unmounted.

<script>
    import tippy from 'tippy.js';
    import 'tippy.js/dist/tippy.css';
    import 'tippy.js/themes/material.css';

    let content = 'Hello!';

    function tooltip(node, options) {
        const tooltip = tippy(node, options);

        return {
            update(options) {
                tooltip.setProps(options);
            },
            destroy() {
                tooltip.destroy();
            }
        };
    }
</script>

<input bind:value={content} />

<button use:tooltip={{ content, theme: 'material' }}>
    Hover me
</button>

相关文章

网友评论

      本文标题:Getting started with Svelte - Ac

      本文链接:https://www.haomeiwen.com/subject/hdjdgdtx.html