如何在不执行任何操作的情况下使用 amp-state 获取数据

how to get data using amp-state without any action

我正在尝试使用 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?

how to get data using amp-state without any action ?

如文档中所述:

amp-bind expressions are not evaluated on page load, but only after an user interaction has taken place. Initial amp-list content still needs to be fetched from a JSON endpoint.

Link: 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>

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