1

I have been trying to show my modal but for some reason it keeps saying that the property isn't defined even though I have declared it in the Data()

I feel like I am missing something critical to my understanding on how this all works ...

The property is defined as false on load and should turn to true on click of the button.

 <template>
  <div class="product-item">
    <h3>{{product.name}}</h3>
    <p>{{product.tagline}}</p>
    <img class="product-image" :src="product.image_url">
    <p>PH: {{product.ph}}</p>
    <button class="show-modal" @click="showModal = true">Show a tip</button>
    <modal v-if="showModal" @close="showModal = false"></modal>
  </div>
</template>

<script>
import Modal from "@/components/Modal.vue";
export default {
  components: {
    Modal
  },
  Data() {
    showModal: false
  },
  props: {
    product: {
      type: Object
    }
  },
  methods: {},
  computed: {},
  mounted() {}
};
</script>

2 Answers 2

3

You data Object should be returned via a function like :

data(){
    return{
      showModal: false
        }
   }

data should be in lowercase .

Sign up to request clarification or add additional context in comments.

Comments

0

A component’s data option must be a function, so that each instance can maintain an independent copy of the returned data object:

data: function () { return { ... }, }

import Modal from "@/components/Modal.vue";
export default {
  components: {
    Modal
  },
  data: function () {
    return {
      showModal: false
    },
  }
  props: {
    product: {
      type: Object
    }
  },
  methods: {},
  computed: {},
  mounted() {}
};
<template>
  <div class="product-item">
    <h3>{{product.name}}</h3>
    <p>{{product.tagline}}</p>
    <img class="product-image" :src="product.image_url">
    <p>PH: {{product.ph}}</p>
    <button class="show-modal" @click="showModal = true">Show a tip</button>
    <modal v-if="showModal" @close="showModal = false"></modal>
  </div>
</template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.