2017-05-12 34 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