The last article talked about Vue3Composition API
Has been widely recognized by our partners. The link is as follows:
Another night, this comic-API still short
As a fighter with pursuit, I still want to play a bullet to everyone from the principle of why there are these restrictions and constraints, is the so-called know what you know why.
The contents of this paper include:
- How can Composition and Options coexist?
- Where does this in setup point to?
- What are the parameters to setup?
- The execution time of setup
- How are lifecycle hooks implemented in Setup?
Next, the official performance of the lightning five whip, we point to stop
First whip: How to achieve harmony between Composition and Options?
Xiao Right introduced composition API in VUE3. At first, many people were afraid that it would affect the writing method of options that we are always familiar with. In fact, the two can coexist and be very harmonious. Small right this punch is clearly the point ~
Here’s how the two can coexist in vuE3:
Conclusion: Here we know vue3’s logic for setup and other options: If setup is set, it is called first, and the other options are processed later.
But today’s young people do not speak martial arts, they may not according to the routine boxing, such as:
// Foo is defined in both data and setup.
createApp({
data() {
return {
foo: 'foo'}},setup(props, ctx) {
const foo = ref('foo in setup')
return { foo }
}
}).mount('#app')
Copy the code
Righty is not a fake master. He will flash
Second whip: What does this in setup point to?
Let’s see what happens when setup is called:
Conclusion: So in setupthis
Is the context in which it is executed, if it isesm
Way to pack, it’s going to beundefined
; If running in single-file mode, yeswindow
; But it doesn’t mean anything anyway, so just forget it, okaythis
Oh, don’t worry about it anymore.
Third whip: What are the arguments to setup?
Let’s look at what is passed when setup executes:
Parameter 4 starts with instance.props and setupContext
Look again at setupContext
Conclusion: Parameter 1 is the component property object, which isProxy
Object, so we can’t deconstruct it otherwise it will be unresponsive; Parameter 2 is an object,attrs
Access to all features of the component,slots
You can access slot content, including normal and scoped slots, which are functions that can only be retrieved after executionvnode
The array.emit
Used to send custom events.
// Can't deconstruct props, can deconstruct setupContext
setup({ foo, bar }, { attrs, slots, emit }) {
watchEffect(() = > {
console.log(foo, bar) // Foo, bar changes, no output})}Copy the code
Fourth whip: The execution time of setup
Setup is executed very early, even before beforeCreated:
Conclusion:setup
There is nobeforeCreate
andcreated
, relevant code is writtensetup
Inside will do; The component instance is passed in when setup is executed, so it can be passed ingetCurrentInstance()
Access it.
Fifth Whip: How are lifecycle hooks implemented in SETUP?
Setup has the flexibility to use lifecycle hooks, which can be written multiple times and executed in the order of registration:
setup() {
onMounted(() = > {})
onMounted(() = >{})}Copy the code
How does this work? Let’s look at the implementation of a function like onMounted:
Take a look at the aliases for these hooks
Finally, of course, are the hook calls
Iterate over them to execute them
So let’s stop here with the five lightning bolts of composition, what else are we going to do? Let me know in the comments section.
If you like to watch video learning, welcome to Yangcun, this article will also have a video explanation.
Young people should speak martial virtue!! “Like”, “follow” and “collect” should be arranged!!
-
This is element3, the open source project of our Flower Mountain front end team
-
A front-end component library that supports VUE3