Vue.JS数据属性未定义

时间:2019-08-06 10:04:20

标签: javascript vue.js vuejs2

我一直试图显示我的模态,但是由于某些原因,它一直说该属性未定义,即使我在Data()中声明了该属性

我觉得我缺少对我的理解至关重要的东西……

该属性在加载时定义为false,单击该按钮后应变为true

 <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 个答案:

答案 0 :(得分:3)

您的数据对象应该通过以下函数返回:

data(){
    return{
      showModal: false
        }
   }

data应该为小写。

答案 1 :(得分:0)

组件的数据选项必须为function,以便每个实例都可以维护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>