如何在vue3应用程序和组件中正确使用dayjs

我可以dayjs通过将其添加到 vue3 组件中来使用它data()


    import dayjs from 'dayjs'
    
    export default {
      data() {
        return {
          dayjs
        }
      }
    }

然后我将能够在模板中使用它,但这是正确的方法吗?

如果我想配置dayjs并全局使用它怎么办?我试过

    import dayjs from 'dayjs'
    import { createApp } from 'vue'
    const app  = createApp(App)
    
    app.use(dayjs) // doesn't work
    app.dayjs = dayjs // doesn't work
    
    app.mount("#app')

但到目前为止无法让它工作。
正确的做法是什么?

回答

你可以使用

import dayjs from 'dayjs'
import { createApp } from 'vue'
const app  = createApp(App)
    
app.config.globalProperties.$dayjs = dayjs
    
app.mount("#app')

  • mmm, best practice use `app.use()`, but it need to adapt dayjs lib.
    `app.config.globalProperties` normal practice.

    in component js u can use `this.$dayjs`, in template `$dayjs`


以上是如何在vue3应用程序和组件中正确使用dayjs的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>