What is the order in which render functions and evaluation attributes are executed in vuejs?

The render functions and evaluation properties in

vuejs are functions. What is the order in which they are executed?

for example, the following code:


new Vue({
    el: "-sharpapp",
    data: {
        content:"main"
    },
    computed:{
        viewComponent(){
            console.log("viewComponent")
            return {
                template:`<div>${this.content=="main"?"":"404"}
                    <br><br>
                    <button data-content="main" v-on:click="handleClick"></button>
                    <button data-content="404" v-on:click="handleClick">404</button>
                    </div>`,
                methods:{
                    handleClick(event){
                        let {target:{dataset:{content}}}=event;
                        this.$root.content=content;
                    }
                }
            }
        }
    },
    render(h) {
        console.log("render")
        return h(this.viewComponent)
    },
    methods:{

    }
})
The code above has two console.log statements.
what I understand is that h (this.viewComponent) in the render function is dependent on viewComponent. You can only know what the final rendered template looks like until you let viewComponent run and return the latest result. If the render function runs first and viewComponent has not responded to the update, then the rendering result is wrong. So my expected result is:

viewComponent
render

but the code I tested, the result is:

clipboard.png

The

render function runs before viewComponent. That"s strange. Why?
did an online test demo, as follows:
https://codepen.io/quiettroja.

Aug.24,2021

computed does not need to be calculated first, but only when it is used, so execute the template first, and then calculate


specifically, you can refer to the life cycle: the computed attribute is called after the vnode virtual node created () life cycle completes.
then $mounted mount
besides, why do you always ask some amazing questions O.O

< hr >

I looked at the code carefully and found that the problem was here:

// computed
computed: {
}

your computed is relative to the root node, so you perform this calculation after the root node created .
then returns another node in the calculation (this node has its own life cycle again).

so in fact, your root node render is only a active render node operation.

the specific process is as follows:

  1. when the root node created is finished, before mounting, vue finds that you have actively called the render function , so go to the render function first.
  2. enter the render function and find that you actively render a calculation attribute of viewComponent , because is passed as a function parameter (if you don't use this viewComponent attribute as a parameter, you can't trigger the calculation. The key point is this, which is also where you are confused, because it is the calculation triggered by the render function, so you have the relationship of render first and then calculation.), so it triggers computed . Then mount $mounted after calculation
  3. every time you click on an event, the updated in the hook is triggered to update the view. At this time, the previous operation is repeated, and this is roughly the process ( of course there is a life cycle in your own template )
  4. .

answer questions:
computed is evaluated only when needed. This is used in the render function, of course, first render and then computed .

1: when does the framework handle computed attributes?

  • between the beforeCreate and created lifecycles, initialize and mount the properties in computed , that is, in the created hook, you can access the calculated properties through this .
  • The computed attribute is obtained only when it is used.

2: the render attribute is first executed between the beforeMount and mounted lifecycles.

Menu