Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
  • 自定义时间处理指令

自定义时间处理指令

新建 timeFormater 文件

import dtime from "time-formater";
function formatTime(app: any) {
  app.directive("format-time", {
    created(el: any, bindings: any) {
      bindings.formatString = bindings.formatString ?? "YYYY-MM-DD HH:mm:ss";
      if (bindings.value) {
        bindings.formatString = bindings.value;
      }
    },
    mounted(el: any, bindings: any) {
      const textContent = el.textContent;
      if (!textContent) {
        el.textContent = "--";
      } else {
        textContent = Number(textContent);
        el.textContent = dtime(textContent).format(bindings.formatString);
      }
    }
  });
}

export default formatTime;

在 main.ts


import formatTime from "./utils/timeFormater";

const app = createApp(App);

formatTime(app);

app.use(ElementPlus).use(pinia).mount("#app");

vue 文件中使用

 <div v-format-time>{scope.row.publishTime}</div>


 
 <div v-format-time="YYYY-MM-DD">{scope.row.publishTime}</div>
Edit this page
最近更新: 2025/6/27 02:24
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人