[Vue 警告]:缺少必需的道具:“productInfo”

dmi*_*er1 1 vue.js

我对 Vue 相当陌生,所以这可能很明显,但我一定遗漏了一些东西。我不断收到错误:[Vue warn]: Missing required prop: "productInfo"在我的 .vue 文件中。它说它在 ProductSlider.vue 中找到。

产品滑块.vue

    <template>
  <div
    id="recommendedProducts">
    <h2>{{ params.headerText }}</h2>
    <div
      class="wrapper">
      <div class="carousel-view">
        <carousel
          :navigation-enabled="true"
          :min-swipe-distance="1"
          :per-page="5"
          navigation-next-label="<i class='fas fa-3x fa-angle-right'></i>"
          navigation-prev-label="<i class='fas fa-3x fa-angle-left'></i>">
          <slide
            v-for="product in products"
            :key="product.id">
            <Product
              :id="product.id"
              :product-info="product"/>
          </slide>
        </carousel>
      </div>
    </div>
  </div>
</template>

<script>
  import Slider from './Slider'
  import Product from './Product'
  import {mapState, mapGetters} from 'vuex'
  import axios from 'axios';

  export default {
    name: "Slider",
    components: {
      Product
    },
    props: {
      productInfo: {
        type: Object,
        required: true
      }

    },
Run Code Online (Sandbox Code Playgroud)

我砍掉了 ProductSlider 代码的结尾,因为其余的很长而且无关紧要。

然后这是我的 Product.vue:

<template>
  <Product>
    <div class="img-container text-center">
      <a
        :href="productInfo.href"
        class="handCursor"
        tabindex="0">
        <img
          v-if="productInfo.imgOverlay"
          :src="productInfo.imgOverlayPath"
          :alt="productInfo.title"
          :aria-label="productInfo.title"
          border="0"
          tabindex="-1">
        <img
          :src="productInfo.imgURL"
          :alt="productInfo.title">
      </a>
    </div>
    <h4 class="itemTitle">{{ productInfo.title }}</h4>
    <div class="price-div">
      <div class="allPriceDescriptionPage">{{ productInfo.price }}</div>
    </div>
    <a
      href="#"
      tabindex="0"
      name="instantadd">
      <div class="btn_CA_Search buttonSearch gradient"> Add to Cart</div>
    </a>
  </Product>
</template>

<script>
  export default {
    name: "Product",
    props: {
      productInfo: {
        type: Object,
        required: true,
      },
    },
  }
</script>
Run Code Online (Sandbox Code Playgroud)

我有productInfo道具,所以我不确定为什么我一直收到这个错误。

Psi*_*dom 7

我认为你混淆props了子组件的props,当你有:

<Product
  :id="product.id"
  :product-info="product"/>
Run Code Online (Sandbox Code Playgroud)

你productInfo在子组件中定义了一个 prop Product,而不是在组件ProductSlider本身中,如果你定义了 prop productInfoin ProductSlider,那么你必须从父组件传递它,也就是说你需要在任何<ProductSlider :product-info="..."/>时候ProductSlider使用它。

注意<Product :product-info="product"/>并不意味着您正在使用 的值productInfo,product-info而是prop在Product组件中定义的名称。

我认为使用一个公平的类比是,如果您将组件视为一个函数,props则是函数参数,并且在调用它们时需要提供必需的函数参数。

对于您的情况,为了消除错误,您可以简单地productInfo从ProductSlider组件中删除prop,因为它没有在该组件中使用。