Home » vue » Unable to access vue data in basic modal

Unable to access vue data in basic modal

Posted by: admin November 26, 2021 Leave a comment


I am trying to loop and reference vue data values within a modal and am getting an error that the field needs to be reactive, despite the values being provided in the component initialization. Any idea why this is?



// register modal component
Vue.component('modal', {
  template: '#modal-template'

// start app
new Vue({
  el: '#app',
  data: {
    myData: [{'first': 'Matt', 'last': 'Smith'}, {'first': 'Tom', 'last': 'Brady'}],
    showModal: false


<script type="text/x-template" id="modal-template">
  <transition name="modal">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">

          <div class="modal-header">
            <slot name="header">
              default header

          <div class="modal-body">
            <slot name="body">
                <li v-for="user in myData"> {{ user.first }} {{ user.last }}</li>

          <div class="modal-footer">
            <slot name="footer">
              default footer
              <button class="modal-default-button" @click="$emit('close')">

<!-- app -->
<div id="app">
  <button id="show-modal" @click="showModal = true">Show Modal</button>
  <!-- use the modal component, pass in the prop -->
  <modal v-if="showModal" @close="showModal = false">

Vue Warning

[Vue warn]: Property or method "myData" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Yous should add myData to modal component props

Vue.component('modal', {
  template: '#modal-template',
  props: ['myData']

and then pass it from parent like this:

  <modal v-if="showModal" :my-data="myData" @close="showModal = false">


corrected demo from Bsalex

<modal v-if="showModal" :my-data="myData" @close="showModal = false">