2017-03-24 15 views
4

イメージファイルパスの配列をコンポーネントに渡しています。 私は別の配列を渡す場合、Vue.jsコンポーネントの小道具の変更を見るための最良の方法は何でしょうか?Vue.jsコンポーネントの小道具の変更を見るには?

私はブートストラップカルーセルを使用していますので、配列が変更されたときに最初のイメージにリセットしてください。 は単純化のためには、私はこれにコード例を削減:

Vue.component('my-images', { 
    props: ['images'], 

    template: ` 
    <section> 
     <div v-for="(image, index) in images"> 
     {{index}} - <img :src="image"/> 
     </div> 
    </section> 
    ` 
}); 
+2

あなたは 'それがであるかのように小道具をwatch'することができますデータ項目。 –

+0

フィードバックは歓迎です! –

答えて

6
<template> 
    <div> 
    {{count}}</div> 
</template> 

<script> 
export default { 
    name: 'test', 
    props: ['count'], 
    watch: { 
    '$props':{ 
     handler: function (val, oldVal) { 
     console.log('watch', val) 
     }, 
     deep: true 
    } 
    }, 
    mounted() { 
    console.log(this.count) 
    } 
} 
</script> 
+0

このコードを少しexpainできますか?すなわち、なぜ/どのように動作するのか? – WhatsThePoint

+0

Omg!この回答は、わずか数十行のコードを節約しました。私は 'componentWillReceiveProps'を探していましたが、Vue.js.私はVueウォッチャーのすべての個々の財産を見なければならなかった。しかし、今私はそれらをすべて単一の機能で見ることができます。 :)ありがとうございます@binaryify –

2

あなたはこれで行ってもいいです:

Vue.component('my-images', { 
    props: ['images'], 
    computed: { 
    imagesComputed: function() { 
     // just an example of processing `images` props 
     return this.images.filter((each, index) => index > 0); 
    } 
    }, 
    template: ` 
    <section> 
     <div v-for="(image, index) in imagesComputed"> 
     {{index}} - <img :src="image"/> 
     </div> 
    </section> 
    ` 
}); 
関連する問題