vue3 组合式API使用全局挂载的 dayjs
vue3+vite+pinia+TS
一、main.ts 挂载
import { createApp } from 'vue'
import '@/styles/style.scss'
import App from './App.vue'
import { setupStore } from '@/store/index'
import 'element-plus/dist/index.css'
import router from './router/index'
import filters from './filters/filters'
import dayjs from 'dayjs'
const app = createApp(App)
app.config.globalProperties.$filters = filters;
app.config.globalProperties.$dayjs = dayjs; // 全局挂载
setupStore(app)
app.use(router).mount('#app')
二、单页面组件获取【组合式API】
<template>
<div>
请看下面的js使用
</div>
</template>
<script setup lang='ts'>
import { getCurrentInstance } from 'vue';
const $dayjs = getCurrentInstance()?.appContext.config.globalProperties.$dayjs;
console.log("我就是dayjs的实例:",$dayjs())
</script>
三、如你有更好的优化方案
虚心求教,请放到评论区