1.安装day.js
npm install -S dayjs
2.组件局部引入
import dayjs from 'dayjs'
若需要在全局引入,可自行在main.js
中导入,并挂在至vue原型上
// 导入day.js
import dayjs from 'dayjs'
Vue.prototype.$dayjs = dayjs
3.项目中使用
<template>
<span id="time">当前时间:{{ nowTime }}</span>
</template>
<script>
import dayjs from 'dayjs'
export default {
data() {
return {
timer:null,
};
},
methods: {
showNowTime() {
this.timer = setInterval(() => {
const date = dayjs(new Date());
this.nowTime = date.format('HH:mm:ss')
}, 1000);
},
},
created() {
this.showNowTime();
},
mounted() {
this.showNowTime()
},