Reputation: 63
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?
Upvotes: 0
Views: 595
Reputation: 46706
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.
Upvotes: 1