简体   繁体   English

Vue.JS数据属性未定义

[英]Vue.JS data property not defined

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() 我一直试图显示我的模态,但由于某种原因,它一直说该属性没有定义,即使我已在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. 该属性在加载时定义为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>

You data Object should be returned via a function like : 您应该通过以下函数返回数据对象:

data(){
    return{
      showModal: false
        }
   }

data should be in lowercase . data应为小写。

A component's data option must be a function , so that each instance can maintain an independent copy of the returned data object : 组件的数据选项必须是一个function ,以便每个实例都可以维护independent copy of the returned data objectindependent 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM