Home > Software engineering >  Issue when trying to pass array value as string via props in Vuejs?
Issue when trying to pass array value as string via props in Vuejs?

Time:03-10

<template>
  <div>
    <div v-for="piza in pizas" :key="piza.pname">
      {{ piza.pname }}
      <List :content="matchpizza" :pname="pizas.pname" :qname="quantitys.qname" />
    </div>
  </div>
</template> 

<script>
import List from "./List.vue";
export default {
  name: "HelloWorld",
  components: {
    List,
  },
  data() {
    return {
      pizas: [
        {
          pname: "chicken pizza",
        },
        {
          pname: "chicken pizza spl",
        },
        {
          pname: "mutton pizza",
        },
        {
          pname: "plain pizza",
        },
      ],

      quantitys: [
        {
          qname: "one",
        },
        {
          qname: "*two",
        },
        {
          qname: "*three",
        },
        {
          qname: "*four",
        },
        {
          qname: "*five",
        },
      ],

      matchpizza: [
        {
          matchnum: "1",
          availability: "later",
          pname: "drone piza",
          qname: "nine",
        },
        {
          matchnum: "2",
          availability: "buy now",
          pname: "chicken pizza",
          qname: "one",
        },
      ],
    };
  },
};
</script>

<template>
  <div>
    <div v-if="matchpizza.length > 0">
      <div
        v-for="match in matchpizza"
        :key="match.matchnum"
        :
      >
        <div v-for="quantity in quantitys" :key="quantity.qname">
          {{ quantity.qname }}
        </div>
        <div >{{ match.availability }}</div>
      </div>
    </div>
    <div v-else ><p>No availability</p></div>
  </div>
</template>
<script>
export default {
  components: {},
  props: {
    content: {
      type: Array,
      required: true,
    },
    pname: {
      type: String,
      required: true,
    },
    qname: {
      type: String,
      required: true,
    },
  },
  data: function () {
    return {};
  },
  methods: {},
  computed: {
    quantitys() {
      return this.content.filter((a) => a.qname === this.qname);
    },
    matchpizza() {
      return this.content.filter((a) => a.pname === this.pname);
    },
  },
};
</script>
<style scoped>
.next-data {
  display: flex;
  flex-direction: row-reverse;
  margin-top: -2%;
  margin-right: 40%;
  color: blue;
}
.next-data-two {
  display: flex;
  flex-direction: row-reverse;
  margin-top: -1%;
  margin-right: 42%;
  color: red;
  margin-bottom: 1%;
}
</style>

Issue when trying to filter the **matchpizza array for pname and qname array value.

Same array values 'pname and qname' having in pizzas and quantitys array.

condition is like:- if in matchpizza array, if i have same array value 'pname and qname' which has already exists in the pizzas and quantitys array, then i need to show the value. or else condition.

Not sure , what i have written is correct logic for that?

Errors:-

[Vue warn]: Invalid prop: type check failed for prop "pname". Expected String, got Undefined

[Vue warn]: Invalid prop: type check failed for prop "qname". Expected String, got Undefined

code (errors in console) :- https://codesandbox.io/s/data-display-tcr8oj?file=/src/components/List.vue

CodePudding user response:

You were not accessing the values by index or the correct loop item.

Here is the fixed version of your HelloWorld.vue file.

<template>
  <div>
    <div v-for="(piza, index) in pizas" :key="piza.pname">
      {{ piza.pname }}
      <List
        :content="matchpizza"
        :pname="piza.pname"
        :qname="quantitys[index] ? quantitys[index].qname : ''"
      />
    </div>
  </div>
</template> 

CodePudding user response:

You have mistyped the passing data and quantities is array you need to loop over it. Look at your props it should be pizas.pname and quantitys[index].qname you are missing the trailing 's'

List component

<List :content="matchpizza" :pname="pizas.pname" :qname="quantity.qname" />

Object

  pizas: [
        {
          pname: "chicken pizza",
        },
        {
          pname: "chicken pizza spl",
        },
        {
          pname: "mutton pizza",
        },
        {
          pname: "plain pizza",
        },
      ],
    
      quantitys: [
        {
          qname: "one",
        },
        {
          qname: "*two",
        },
        {
          qname: "*three",
        },
        {
          qname: "*four",
        },
        {
          qname: "*five",
        },
      ],
  • Related