代码之家  ›  专栏  ›  技术社区  ›  AnchovyLegend

无法在基本模式中访问vue数据

  •  0
  • AnchovyLegend  · 技术社区  · 8 年前

    我试图在一个模式中循环和引用vue数据值,并得到一个错误,即字段需要反应,尽管组件初始化中提供了值。知道为什么吗?

    小提琴: https://jsfiddle.net/mwLbw11k/2618/

    JS公司

    // 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
      }
    }) 
    

    HTML

    <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
                </slot>
              </div>
    
              <div class="modal-body">
                <slot name="body">
                  <ul>
                    <li v-for="user in myData"> {{ user.first }} {{ user.last }}</li>
                  </ul>
                </slot>
              </div>
    
              <div class="modal-footer">
                <slot name="footer">
                  default footer
                  <button class="modal-default-button" @click="$emit('close')">
                    OK
                  </button>
                </slot>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </script>
    
    <!-- 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">
      </modal>
    </div>
    

    Vue警告

    [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.
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Bsalex    8 年前

    您应该添加 myData modal 组件道具

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

    然后像这样从家长那里传过来:

      <modal v-if="showModal" :my-data="myData" @close="showModal = false">
      </modal>
    
        2
  •  1
  •   Volodymyr Symonenko    8 年前

    已更正 demo 来自Bsalex

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