如何查看 Vue.js 组件中的 prop 变化?

新手上路,请多包涵

我将一组图像文件路径传递给一个组件。我想知道如果我传递一个不同的数组,在 Vue.js 组件中观察道具变化的最佳方式是什么?

我正在使用引导轮播,所以想在数组更改时将其重置为第一张图片。为了简单起见,我将代码示例简化为:

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

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

原文由 Tadas Majeris 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 316
2 个回答
<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>

原文由 Binaryify 发布,翻译遵循 CC BY-SA 3.0 许可协议

你很高兴这样做:

 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>
  `
});

原文由 Jonatas Walker 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题