What did New Vue do? 🙄
A lot of times we’re asked is there anything wrong with the kidney in New Vue? This article we will have a good look!! 😁
Let’s start with source code debugging
A previous article covered vue source debugging, which is a good place to start.
Entrance to the file
When run dev is executed, we can retrieve the parameters according to the package.json configuration
"dev": "rollup -w -c scripts/config.js --sourcemap --environment TARGET:web-full-dev",
At this point we can get a path scripts/config.js and the parameter TARGET:web-full-dev
inscripts/config.js
Files can be seen in the defined Builds object foundweb-full-dev
The corresponding property valueThis is where the entry file is foundsrc\platforms\web\entry-runtime-with-compiler.js
What does the entry file do?
You can see the extension in the code above$mount
Next, we get the DOM element based on the parameter el passed in. And determine whether or notrender
, if notrender
Is to findtemplate
, if nottemplate
According to the functiongetOuterHTML
willel
Internal content astemplate
So we can conclude thatrender template el
The priority of isrender > template > el
. iftemplate
You need a functioncompileToFunctions
willtemplate
Compile to get arender
Delta function, and put therender
Set to component options. And it was executedmount
Method.
Let’s get to the subject
Look for the constructor Vue
You can see from the entry file./runtime/index
File to importVue
To enter the file.You can seecore/index
File importedVue
You can also see the implementation$mount
Methods.
Enter the filecore/index
You can see from./instance/index
In the importVue
And a global Api initialization method is performedinitGlobalAPI
, continue to enter./instance/index
Finally found itVue
Constructor. inVue
Constructor_init
Methods. A series of blends are also performed below. Can be found ininitMixin
Found in the_init
Methods. So let’s see_init
What did the method do?
What does _init in the constructor do?
Here we can see the sequence of processes very clearly. The merging of attributes is followed by various initializations.
initLifecycle
– > definition$parent
,$children
,$root
,$ref
initEvent
-> Listen for some custom eventsinitRender
– > definition$slots
,$createElement
And let$attrs
$listeners
Become responsive- Execution life cycle
beforeCreate
initInjections
-> Get grandparent injected datainitState
-> Data initialization:data
,props
,methods
,computed
,watch
initProvide
-> Provide the data to future generations- Execution life cycle
created
- Perform a mount
How to mount
Back to the implementation I just mentioned$mount
Method filecore/index
A slave file is returnedcore/instance/lifecycle
functionmountComponent
The first thing we do before we execute this function is we extend it$mount
Delta function, you can get it thererender
. inmountComponent
To perform thebeforeMount
Hook, and thennew Watcher
performupdateComponent
Function. The main thing that this function does is pass_render()
functionrender
Into the virtual DOM, through_update
Insert the virtual DOM into the real DOM and execute itmount
Hook function
conclusion
The above is probably my understanding of the new Vue dry a series of operations, if there is any improvement, welcome to leave a message oh ~~~ finally come to a mind map