amp-html - 如何在不执行任何操作的情况下使用 amp-state 获取数据
问题描述
我正在尝试使用 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-bind 表达式不会在页面加载时评估,而是仅在发生用户交互后评估。仍然需要从 JSON 端点获取初始 amp-list 内容。
链接: https ://amp.dev/documentation/examples/components/amp-list/?format=websites#rendering-amp-state
这部分:[text]="currentTime.time"
与amp-bind有关。因此,这是不可能的。使用放大器列表:
<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>
然后它将在加载页面后立即显示并在单击按钮时更新。