指令现在已完成。让我们看看可以在哪里以及如何使用它。

在我们的示例元素模板中,我们一直将 timeAgo 指令作为字符串渲染到子表达式中:

<p>This page was rendered ${timeAgo(timeCreated)}.</p>

然而,我们的指令可以在_任何_表达式类型中使用,包括属性表达式属性值表达式。假设我们有一个 <comment-card> 元素,它接受 subjectdescriptiontime 等字段。我们已经为你导入了一个,所以在模板中也添加这个:

<comment-card user="litdeveloper"
time="12:49 pm"
subject="Just tried AsyncDirectives!"
content="Cool stuff, they're really powerful!">
</comment-card>

现在,由于该元素接受时间作为字符串,我们可以将 timeAgo 指令绑定到该属性上,它将定期设置元素的 time,而无需外部模板重新渲染:

time=${timeAgo(timeCreated)}

恭喜!你现在有了一个可用的、实用的 AsyncDirective

进一步学习的想法: