Skip to content
Advertisement

How to import Library into Vue3 project

could someone help me import a library to my vue3 project so that I can use it in all components?…

I’am trying to import ‘moments.js’ to my project

  • Its installed with npm
  • in my ‘main.js’ (entry) I import it like:
import { createApp } from "vue"
import App from "./App.vue"
import moment from "moment"
const app = createApp(App)
app.use (moment)
app.mount("#app")

but when I try to console.log(this.moment) from another component I get errors that this.moment is not a function

Advertisement

Answer

For anyone stumbling onto this post. I changed the code to:

import { createApp } from "vue"
import App from "./App.vue"
import moment from "moment"
const app = createApp(App)
app.provide("moment", moment)
app.mount("#app")

inside other components:

export default {
   inject: ["moment"],
// Other code can now use "moment"
}

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement