vue3 <script setup> 语法糖时间组件

html 复制代码
<template>
  <div>
    <p>当前时间Current Time: {{ currentTime }}</p>
  </div>
</template>

<script setup>
import { ref, onBeforeUnmount, onMounted } from 'vue'

const currentTime = ref('')

let interval // 声明 interval 变量

const getToday = () => {
  const datas = new Date()
  const on2 = ' : '
  let onS = datas.getHours()
  let onF = datas.getMinutes()
  let onN = datas.getSeconds()

  if (onS >= 0 && onS <= 9) {
    onS = '0' + onS
  }
  if (onF >= 0 && onF <= 9) {
    onF = '0' + onF
  }
  if (onN >= 0 && onN <= 9) {
    onN = '0' + onN
  }

  currentTime.value = onS + on2 + onF + on2 + onN
}

onMounted(() => {
  interval = setInterval(() => {
    // 将 interval 赋值为 setInterval 的返回值
    getToday()
  }, 1000)
})

onBeforeUnmount(() => {
  clearInterval(interval) // 使用 clearInterval 清除 interval 计时器
})
</script>
<style scoped>
p {
  font-size: 30px;
  color: hsla(160, 100%, 37%, 1);
    text-shadow: 1px 1px 1px rgb(0, 0, 0);
}
</style>
相关推荐
炫饭第一名27 分钟前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫43 分钟前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊43 分钟前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter1 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折1 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_1 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码11 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial1 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu2 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端
jiayu2 小时前
Angular6学习笔记13:HTTP(3)
前端