preface
Vue source code analysis will be conducted in succession recently, a series of courses are as follows:
The state series
- Principle of props
- Principle of the methods
- Principle of the data
- Principles of the computed
Method execution timing
After beforeCreate and before created, initState is called
InitProps is called in initState
Source code analysis
initProps
The code above does three things:
- Traverse the props
- Set the props to reactive
- Set up the props agent
defineReactive
Set the props to reactive
- Object.defineproperty-get, for dependency collection;
- Object.defineproperty-set, used for dependency updates;
- Each property declared by data has its own dependency collector subs;
- The dependency collector subs holds the dependency watcher;
- Watcher can be used for view updates;
proxy
Set up the props agent
If you want to call this. XXX for Props, you are actually calling this._props.
- Use props as placeholder on the VM so that the props can be accessed in the form of vm. XXX.
- DefineProperty get and set to get and assign vm._props indirectly. All operations on the props are forwarded to vM. _props.