Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
Anurat Chapanond Avatar asked Mar 10 '21 06:03

Anurat Chapanond


People also ask

Does Buefy work with Vue 3?

Buefy (8.7k stars on GitHub) integrates the CSS framework Bulma (43.7k stars on GitHub) into Vue. js 2 projects. However Buefy does not support Vue. js 3 yet.

Does Gridsome support Vue 3?

Gridsome does not support Vue 3 yet. For more info refer to the kanban board for the Vue 3 upgrade on GitHub.

Should I use vue2 or Vue 3?

If you are on a very large and complex existing project using Vue 2: You may not want to migrate to Vue 3, depending on your code, the migration time and performance benefits may not be worth it. If you are facing performance issues after doing various optimizations, then use Vue 3.

Can I use Vue 3 without composition API?

Vue 3 does not require using the Composition API.


3 Answers

u can use

import dayjs from 'dayjs'
import { createApp } from 'vue'
const app  = createApp(App)
    
app.config.globalProperties.$dayjs = dayjs
    
app.mount("#app')
like image 50
Трин Зотофф Avatar answered Oct 20 '22 20:10

Трин Зотофф


The accepted method does not seem to take into account composition API. My understanding is that the only way to use this with Composition API is to provide/inject. Example below working with composition API, options API in script and templates.

//[main.js]

import dayjs from 'dayjs' //import dayjs in your main.js
 
app.provide('dayJS', dayjs) // provide dayJS

app.use(router).mount("#app") // mount app

// [component.js]

// Composition API setup ------------------
import { inject } from 'vue' // import inject from vue
const dayJS = inject("dayJS") // inject dayJS

//make sure that you return dayJS in setup along with any functions/variables
return { dayJS }

// Options API setup ------------------
app.component('mycomponent', {
  inject: ['dayJS'],
  created() {
    console.log(dayJS())  
  }
})

//You can now use dayJS directly within setup ex: dayJS() or template ex: {{dayJS()}}.
like image 20
Neil Hewitt Avatar answered Oct 20 '22 21:10

Neil Hewitt


You can use provide/inject to use dayjs inside of your component.

//main.js
import dayjs from 'dayjs'
import { createApp } from 'vue'

const app = createApp({
  provide() {
    return {
      $dayjs: dayjs // <-- provide 
    }
  },    
    
app.mount("#app')
//myComponent.vue
<template>
    DajsJS: {{ myDays }}
</template>

<script>
export default {
  name: 'myComponent',
  inject: ['$dayjs'], // <-- inject
  computed: {
    myDays() {
      return this.$dayjs('1990-01-01')
    }
  }
}
</script>
like image 2
Hexodus Avatar answered Oct 20 '22 21:10

Hexodus