Search code examples
vue.jsvuejs3dayjs

how to properly use dayjs inside vue 3 app and component


I am able to use dayjs inside vue3 component by adding it to data()

import dayjs from 'dayjs'

export default {
  data() {
    return {
      dayjs
    }
  }
}

Then I will be able to use it inside template but is this the correct way to do?

What if I want to configure dayjs and use it globally? I tried

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')

but couldn't get it to work so far.
What is the correct way to do it?


Solution

  • u can use

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