Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render function in Vue.js

when use render function in Vue.js to render a dom element,I try to rewrite v-if directive with JavaScript’s if like this.

export default{
    destroyed(){
        console.log("destroyed")
    },
    props:['show'],
    render(h){
        if(this.show){
            return h('div',{domProps:{innerHTML:'test'},on:{click:this.quit}})
        }
    },
    methods:{
        quit(){
            this.$destroy();
        }
    }
}

But when show is false, it seems to Vue instance won't go to destory lifecycle.

If I use vm.$destory method,the instance go to destory lifecycle,but the dom element still exists.

how dose it happen?

like image 823
chickenDinner Avatar asked Feb 05 '26 05:02

chickenDinner


1 Answers

export default{
    destroyed(){
        console.log("destroyed")
    },
    props:['show'],
    render(h){
        if(this.show){
            return h('div',{domProps:{innerHTML:'test'},on:{click:this.quit}})
        }else { 
          return null 
        }
    },
    methods:{
        quit(){
            this.$destroy();
        }
    }
}

Here is the working fiddle: https://jsfiddle.net/srinivasdamam/3s18pjrg/

like image 103
Srinivas Damam Avatar answered Feb 07 '26 19:02

Srinivas Damam