【问题标题】:AlpineJS: How to pass a x-for variable to an x-data functionAlpineJS:如何将 x-for 变量传递给 x-data 函数
【发布时间】:2022-03-01 04:56:18
【问题描述】:

你能帮忙吗?

我正在尝试根据循环中的某些属性添加倒计时,但我找不到任何方法(不是通过尝试也不是通过谷歌搜索)如何在我的函数中传递该值:

    <template x-for="item in cartData.items">
       [...]
        <template x-if="item.product_type == 'test'">
            <div  x-data="getCountdown()" x-init="init()">
                <span x-text="timeLeft(item.timerEnd)"></span>
            </div>
            <script type="text/javascript">[...]
            </script>
        </template>
    </template>

我试图将 item.timerEnd 传递给每个函数(getCountdowninittimeLeft),但我总是收到 item 未定义的错误,如果我传递它,例如。至 &lt;span x-text="new Date(item.timerEnd).toLocaleString()"&gt;&lt;/span&gt; 这行得通。

我错过了什么?

PS:感谢fpr在这里的第一个帮助:How to make timer in alpine.js app with time interval

【问题讨论】:

  • x-for 必须只有一个根元素,根据 this doc

标签: alpine.js


【解决方案1】:

这样做$data.item.timerEnd

像这样:

<template x-for="item in cartData.items">
       [...]
        <template x-if="item.product_type == 'test'">
            <div  x-data="getCountdown($data.item.timerEnd)" x-init="init($data.item.timerEnd)">
                <span x-text="timeLeft($data.item.timerEnd)"></span>
            </div>
            <script type="text/javascript">[...]
            </script>
        </template>
    </template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2020-11-25
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多