0

These are some of the ways I know to create a global function to vue js project:

time.js

// if Vue.use()

export default {
  install: Vue => {
    Object.defineProperty(Vue.prototype, "time", {
       return new Date().getTime();
    })
  }
}

// else 

function time () {
  return new Date().getTime();
}

export { time }

main.js

...

import { time } from "time";

// if Vue.prototyp
Vue.prototype.$time = time 

// else if Vue.use()
Vue.use(time)

...

App.vue

// if Vue.prototype or Vue.use()
console.log(this.$time());

// else 
import { time } from "time";
console.log(time());

What is the best method for vue js project?

1 Answer 1

1

Use import rather than polluting the global scope.
You will not benefit from tree-shaking, will probably have collisions, it's harder to debug and probably some other drawbacks that I cannot think about right now.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.