admin管理员组

文章数量:1394611

In the AlpineJS docs there's this example:

<div x-data="dropdown()">
    <button x-on:click="open()">Open</button>

    <div x-show="isOpen()" x-on:click.away="close()">
        // Dropdown
    </div>
</div>

<script>
    function dropdown() {
        return {
            show: false,
            open() { this.show = true },
            close() { this.show = false },
            isOpen() { return this.show === true },
        }
    }
</script>

Now what I would like to do is within that <script> have another function that calls close(), so something like:

function aNewFunction()
{
  close();
}

In the AlpineJS docs there's this example:

<div x-data="dropdown()">
    <button x-on:click="open()">Open</button>

    <div x-show="isOpen()" x-on:click.away="close()">
        // Dropdown
    </div>
</div>

<script>
    function dropdown() {
        return {
            show: false,
            open() { this.show = true },
            close() { this.show = false },
            isOpen() { return this.show === true },
        }
    }
</script>

Now what I would like to do is within that <script> have another function that calls close(), so something like:

function aNewFunction()
{
  close();
}
Share Improve this question asked Mar 9, 2020 at 13:49 eskimoeskimo 2,6358 gold badges52 silver badges98 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5
dropdown().close

This will return close() if you want to call close then simply do something like this .

dropdown().close()

本文标签: javascriptCall function within function of AlpineJSStack Overflow