簡體   English   中英

vue組件中的props如何使用?

[英]How to use the props in vue component?

我嘗試在計算值中使用道具,但我不斷收到錯誤消息:

[Vue 警告]:渲染錯誤:“類型錯誤:無法讀取未定義的屬性‘長度’”在 ---> 中找到,位於 src/cmps/space-details/space-imgs.vue 位於 src/pages/space-details。 vue 在 src/App.vue

也許我用錯了道具? 組件:

<template>
  <div v-if="imgUrls.length" class="space-imgs" :class="pics">
    <img :src="img" v-for="(img, idx) in imgUrls.slice(0, 5)" :key="idx" />
  </div>
</template>

<script>
export default {
  props: { imgUrls: Array },

  computed: {
    pics() {
      return `pics-${this.imgUrls.length}`;
    },
  },
};
</script>

這就是我傳遞道具的方式:

    <space-imgs :imgUrls="space.imgUrls" />

謝謝

您的模板將在傳遞 prop 之前首先呈現,從而使imgUrls未定義。 所以就這樣做: v-if="imgUrls"

像下面一樣傳遞道具。 這將起作用。

<space-imgs :img-urls="space.imgUrls" />

用於避免組件中錯誤的道具數據。 用默認值初始化 prop。

props: { 
 imgUrls: {
     type: Array,
     default: [],
     required: true
 } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM