undefinedfix
Sign in

How to define a global const or method, object JS, in Vue project

jthwr edited in Wed, 02 Nov 2022

Similar to global methods, variables, components and function libraries, they can be directly used anytime and anywhere without import and require?

For example, if I encapsulate localstorage, I don't want to import any more, just like storage. I can use methods

Is this mode good

3 Replies
hainesm6
commented on Wed, 02 Nov 2022

Depending on your requirements, you can mount these tools and methods to the Vue prototype in the entry file Vue.prototype. $storage = storage, so that each component can be used in this. $storage mode. If it is a global variable, it can be mounted on the window, so it can also be used directly.

ThaSami
commented on Wed, 02 Nov 2022

Single file introduction, not many places to use, recommend. Global introduction is used in many places, and it is necessary to do so in some special scenarios.

thabouti
commented on Wed, 02 Nov 2022

Put it in vuex