Not by prop
and inherit
.
For example, I want to define my general output error function to log the promise
catch error, but I have to import this output error function from helper.js in all components. When the page system is huge, it is hard to maintain. What I want is define a global var or method in parent, all child components can use it.
Now my temporary solution is to user webpack-stream or gulp to package the helper.js with main file. But should there be a way in Vue to manage the global var and function in the component symtem?
There are several ways:
helper.js
when you need it. This is actually the recommended approach - although repetitive, it clearly shows where the method comes from.Vue.prototype
. For example, Vue.prototype.$onPromiseError = ...
so all instances can just this.$onPromiseError(err)
. Better yet, group all your global helpers into a mixin, so you know where to look for them and maintain them.@yyx990803 Big thank-you! I was trying to directly assign properties to the Vue instance and going nuts. :smile:
Most helpful comment
There are several ways:
helper.js
when you need it. This is actually the recommended approach - although repetitive, it clearly shows where the method comes from.Vue.prototype
. For example,Vue.prototype.$onPromiseError = ...
so all instances can justthis.$onPromiseError(err)
. Better yet, group all your global helpers into a mixin, so you know where to look for them and maintain them.