最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing multiple parameters on Svelte action - Stack Overflow

programmeradmin3浏览0评论

Accordingly to the Svelte documentation:

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

I want to pass multiple parameters to a Svelte action function, but only the last one is recogonized

DEMO

<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>

Is there any viable solution that avoid the use of a single object as parameter?

<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>

Accordingly to the Svelte documentation:

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

I want to pass multiple parameters to a Svelte action function, but only the last one is recogonized

DEMO

<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>

Is there any viable solution that avoid the use of a single object as parameter?

<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>
Share Improve this question asked Nov 24, 2019 at 20:54 henriquehbrhenriquehbr 1,1224 gold badges23 silver badges43 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The content between {} can be any JavaScript expression, and when you write 'a', 'b' you are using the ma operator, so the value of the entire expression will be 'b'.

You could use an array instead.

Example (REPL)

<script>
  function example(node, [arg1, arg2]) {
    console.log(arg1, arg2)
    return {
      destroy() {
        // the node has been removed from the DOM
      }
    }
  }
</script>

<h1 use:example="{['a', 'b']}">Hello World!</h1>

You also can use an array.

Below a snippet of my code:

export function initMapDesc(mapMark) {

  // make the entry (obj) and the posed search regex reactive
  return (node, [obj, pRex]) => {
    // the node has been mounted in the DOM
    node.innerHTML = mapObj(node, obj, pRex, mapMark);

    return {
      update([obj, pRex]) {
        node.innerHTML = mapObj(node, obj, pRex, mapMark);
      },
      // destroy the node to clear the view (on session change)
      destroy() {
        node.innerHTML = '';
      }
    };
  };
};

This code renders an object into a table <td> node. The regex stream is used to search nodes and mark the search results.

The code below shows the call of the use function. A closure is used to pass an object to the use function and to receive the regex search results.

const mapMark = {         // mapMark Obj
  markedIds: [],          // marked result row ids 
  skipProps: ['kind', 'method', 'type', 'bic']
};
const mapper = initMapDesc(mapMark); 

and in the HTML:

<td id="{key}" class="space-normal" use:mapper={[$norm.map[key], $pseudoRex]}></td>

I have submitted a proposal to allow object and class methods to be used in a use directive.

发布评论

评论列表(0)

  1. 暂无评论