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

javascript - Call function within function of AlpineJS - Stack Overflow

programmeradmin8浏览0评论

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()
发布评论

评论列表(0)

  1. 暂无评论