用于 Nuxt 的 SSR 安全时间元素
<time> 元素安装并将 nuxt-time 添加到你的 nuxt.config。
npx nuxi@latest module add time
export default defineNuxtConfig({
modules: ['nuxt-time'],
})
要使用,你可以在应用程序的任何地方使用 <NuxtTime> 组件。它将渲染一个符合规范的 <time> 元素。
它接受 datetime 和 locale(可选)属性,以及 Intl.DateTimeFormat 接受的任何属性(请参阅 MDN 参考)。注意:你可以通过 datetime 提供 Date 或 number(例如 Date.now()),它将正确渲染为 ISO 格式的日期。
<template>
<!--
Date.now() will safely respect the time on the server, not on the
client. What's more, there will be no flash between server
and client locale formatting.
-->
<NuxtTime :datetime="Date.now()" second="numeric" month="long" day="numeric" />
</template>
Nuxt Time 还支持使用 Intl.RelativeTimeFormat API 进行相对时间格式化。你可以通过将 relative 属性设置为 true 来启用此功能。
<template>
<!--
This will display the time relative to the current time, e.g., "5 minutes ago"
-->
<NuxtTime :datetime="Date.now() - 5 * 60 * 1000" relative />
</template>
corepack enable 启用 Corepackpnpm install 安装依赖pnpm dev:prepare 模块pnpm dev 以开发模式启动 playground用 ❤️ 制作
根据 MIT 许可证 发布。