如何在不执行任何操作的情况下使用 AMP 状态获取数据



我正在尝试使用 amp-state 从 API 获取值,但问题是您需要执行操作才能获取数据。

<amp-state id="currentTime"
src="/documentation/examples/api/time"></amp-state>
<button on="tap:currentTime.refresh">
Refresh
</button>
<div [text]="currentTime.time"></div>

在上面的示例中,您需要单击按钮刷新才能获得结果,有没有另一种方法可以直接 ger gata 而无需执行任何操作?

如何使用 amp-state 获取数据而无需任何操作?

如文档中所述:

AMP 绑定表达式不会在页面加载时计算,而只会在 用户交互已发生。初始放大器列表内容仍需要 要从 JSON 终结点提取。

链接: https://amp.dev/documentation/examples/components/amp-list/?format=websites#rendering-amp-state

这部分:[text]="currentTime.time"与放大器绑定有关。因此,这是不可能的。使用放大器列表:

<amp-state id="currentTime" src="/documentation/examples/api/time"></amp-state>
<amp-list height="100" items="." src="/documentation/examples/api/time" single-item [src]="currentTime">
<template type="amp-mustache">
<div [text]="currentTime.time">{{time}}</div>
</template>
</amp-list>
<button on="tap:currentTime.refresh">Refresh</button>

然后它将在加载页面后立即显示,并在单击按钮时更新。

最新更新