Skip to content

Commit 42a1087

Browse files
committed
feat(dayjs): dayjs 引入并全局挂载
1 parent 8beceba commit 42a1087

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ vue-h5-template
8383
- [vue-router](#router)
8484
- [本地存储 storage 封装](#storage)
8585
- [哀悼模式](#mourn)
86+
- [dayjs 处理时间](#dayjs)
8687

8788
### <span id="env">⚙️ 环境变量配置</span>
8889

@@ -841,6 +842,7 @@ router.beforeEach((to, from, next) => {
841842
### <span id="mourn">🕯️ 哀悼模式</span>
842843

843844
通过 filter.grayscale() 实现页面哀悼模式
845+
844846
2022/11/30 祝长者一路走好 👓🕯️
845847

846848
```css
@@ -859,6 +861,31 @@ router.beforeEach((to, from, next) => {
859861
1. 在路由拼接 mourn=true, 项目全页面会进入哀悼模式
860862
2. 通过 `utils/index``isMournMode``mixins.scss` 中的样式,对特定页面开启哀悼模式,如首页 `home/index.vue`
861863

864+
[🔙 返回顶部](#catalogue)
865+
866+
### <span id="dayjs">⚙️ dayjs 处理时间</span>
867+
868+
本项目引入 dayjs 并已在全局挂载,可直接使用
869+
870+
```javascript
871+
<template>
872+
<div class="wrapper">
873+
{{ proxy.$dayjs(new Date()).format('YY-MM-DD HH:mm:ss') }}
874+
</div>
875+
</template>
876+
877+
<script setup>
878+
import { computed, getCurrentInstance } from 'vue'
879+
880+
const { proxy } = getCurrentInstance()
881+
882+
const currentTime = computed(() => {
883+
return proxy.$dayjs(new Date()).format('YY-MM-DD HH:mm:ss')
884+
})
885+
886+
</script>
887+
```
888+
862889
---
863890

864891
# 鸣谢

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"babel-eslint": "^10.1.0",
3838
"babel-plugin-transform-remove-console": "^6.9.4",
3939
"cz-customizable": "^6.3.0",
40+
"dayjs": "^1.11.7",
4041
"eruda": "^2.5.0",
4142
"eslint": "^6.7.2",
4243
"eslint-plugin-import": "^2.20.2",

src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createApp } from 'vue'
22
import App from './App.vue'
33
import router from './router'
44
import store from './store'
5+
import dayjs from 'dayjs'
56

67
import { initDebugTool } from '@/utils/debug'
78
import { initMockServer } from '@/mock/mock-server'
@@ -17,4 +18,7 @@ import '@/router/permission'
1718
initDebugTool()
1819
initMockServer()
1920

20-
createApp(App).use(store).use(router).use(Vant).mount('#app')
21+
const app = createApp(App)
22+
23+
app.config.globalProperties.$dayjs = dayjs
24+
app.use(store).use(router).use(Vant).mount('#app')

src/mock/mock-server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ function mockXHR () {
4040
}
4141

4242
for (const i of mocks) {
43-
Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
43+
Mock.mock(
44+
new RegExp(i.url),
45+
i.type || 'get',
46+
XHR2ExpressReqWrap(i.response)
47+
)
4448
}
4549
}
4650

4751
export function initMockServer () {
4852
const NEED_MOCK = getEnvValue('VUE_APP_NEED_MOCK')
49-
if (NEED_MOCK) {
53+
if (NEED_MOCK === 'true') {
5054
mockXHR()
5155
}
5256
}

0 commit comments

Comments
 (0)