2017-05-12 31 views
0

这是我的模板vuejs子组件生命周期差异根组件?

<parent> 
<child></child> 
</parent> 

,这是孩子的部件脚本

beforeCreate(){ 
    console.log('beforeCreate'); 
} 

created(){ 
    console.log('created'); 
} 

beforeMount(){ 
    console.log('beforeMount'); 
} 

我测试vuejs组件的生命周期。

,所以我写在儿童中的子组件的所有钩子方法,

,但创造钩方法并不称为

是正常或不?

谢谢

回答

0

它应该像其他生命周期方法一样正常工作。我重新测试了你的案例(基于vue cli webpack模板),它工作正常。您是否使用vue-cli或自定义捆绑/构建方式?

请参见下面的示例所示(我想你错过生命周期方法的方括号后的逗号):

<script> 
    // PARENT component 
    import Child from './components/Child' 

    export default { 
    name: 'parent', 

    components: { 
    Child 
    } 
} 
</script> 


<script> 
    // CHILD component 
    export default { 
    name: 'child', 

    beforeMount() { 
    console.log('before mount') 
    }, 

    created() { 
     console.log('created') 
    }, 
    beforeCreate() { 
     console.log('before create') 
    } 
} 
</script> 
+0

我使用VUE类(https://github.com/vuejs/vue-class-component) 。如果你的代码运行良好,那么它是vue类问题..谢谢 – user1110977