Conflicts of vue components

in projects we always define a global variable vm before the script tag in the vue component, then assign a value vm=this in the hook beforeCreate , and then we can use vm anywhere. Like this:

clipboard.png

parammountedvm:

clipboard.png

clipboard.png

test1

clipboard.png

I know that this problem will be solved after using this, and I speculate that it may be the pot of vue-loader, but I also can"t figure out how it works. Please give me your advice!

May.22,2022
The

beforeCreate hook is triggered only as soon as the component is created, and then the component does not trigger without destroying it. So if you write vm in this way, you will only get the context of the component when it was originally created, not the context of the component you are currently using. Put vm = this in the mounted hook to execute.


my understanding is that let vm = null is equivalent to creating a new object globally. After two calls to the test1 component, vm is assigned to the component instance in beforeCreate at the time of the last call, so vm is always the instance of the second test1 component in the mount executed later.

Menu