【问题标题】:calling a method on the root from a component, what is wrong with my code?从组件调用根上的方法,我的代码有什么问题?
【发布时间】:2019-10-31 16:16:08
【问题描述】:

我的引导模式有一个组件。模态有一个标记为“得到它”的按钮,我试图调用在根实例上找到的方法。它不起作用——我无法说出我错过了什么。我添加了一个点击处理程序并发出点击,但无法触发清除功能?请告知有什么问题-谢谢

 
 Vue.component('modal', {
    template: '#modal-template',
    props:{
        bgClass:{
            type:String,
            default:'default'
        },
       
    },
    methods: {
		clickHandler () {
      this.$emit('click');
    }
  
	}
  })
  
  new Vue({
      el: "#app",
      data: function data() {
      return{
      showModalZ:false
      }
      },
       
    methods: {
    
        clear: function(){
        alert("checkme");
        }
        }
       
   
  })
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
       <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>

<script type="text/x-template" id="modal-template">
    <transition name="modal">
      <div class="vm-modal-mask">
        <div class="vm-modal-wrapper">
          <div class="vm-modal-container">
  
            <div class="vm-modal-header">
              <slot name="header">
                default header
              </slot>
            </div>
  
            <div :class="bgClass" class="vm-modal-body">
              <slot name="body">
                default body
              </slot>
            </div>
  
            <div class="vm-modal-footer">
              <slot name="footer">
                &nbsp;
                <button class="modal-default-button btn btn-primary" @click="clickHandler(),clear()">
               Got It!
                </button>
              </slot>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </script>
  <div id="app">
<h5>hello <i style="font-size:20px;cursor:pointer;" aria-hidden="true" class="fa fa-info-circle" v-on:click="showModalZ=true"></i></h5>
               
                    <modal v-if="showModalZ" @close="showModalZ = false">
                            <h5 slot="header"><strong>input goes here</strong></h5> <hr>
                            <div>
                            test
                            </div>
                            
            
                        </modal>
                        </div>

【问题讨论】:

    标签: vue.js components


    【解决方案1】:

    只需使用 this.$parent.$root.methodname() 例如 this.$parent.$root.clear();

    Vue.component('modal', {
        template: '#modal-template',
        props: {
            bgClass: {
                type: String,
                default: 'default'
            },
        },
        methods: {
            clickHandler() {
                this.$parent.$root.clear();
            }
        }
    })
    
    new Vue({
        el: "#app",
        data: function data() {
            return {
                showModalZ: false
            }
        },
        methods: {
            clear: function () {
                alert("checkme");
            }
        }
    })
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
    
    <script type="text/x-template" id="modal-template">
    <transition name="modal">
        <div class="vm-modal-mask">
            <div class="vm-modal-wrapper">
                <div class="vm-modal-container">
    
                    <div class="vm-modal-header">
                        <slot name="header">
                            default header
                        </slot>
                    </div>
    
                    <div :class="bgClass" class="vm-modal-body">
                        <slot name="body">
                            default body
                        </slot>
                    </div>
    
                    <div class="vm-modal-footer">
                        <slot name="footer">
                            &nbsp;
                            <button class="modal-default-button btn btn-primary" @click="clickHandler()">
                                Got It!
                            </button>
                        </slot>
                    </div>
                </div>
            </div>
        </div>
    </transition>
    </script>
    <div id="app">
    <h5>hello <i style="font-size:20px;cursor:pointer;" aria-hidden="true" class="fa fa-info-circle" v-on:click="showModalZ=true"></i></h5>
    
    <modal v-if="showModalZ" @close="showModalZ = false">
        <h5 slot="header"><strong>input goes here</strong></h5> <hr>
        <div>
            test
        </div>
    
    
    </modal>
    </div>

    【讨论】:

      【解决方案2】:

      您需要从模型组件clickHandler 函数发出“关闭”事件,并使用@close 在父级上捕获它

      工作示例:

      Vue.component('modal', {
        template: '#modal-template',
        props: {
          bgClass: {
            type: String,
            default: 'default'
          }
        },
        methods: {
          clickHandler() {
            this.$emit('close');
          }
        }
      })
      
      new Vue({
        el: "#app",
        data: function data() {
          return {
            showModalZ: false
          }
        },
        methods: {
          clear: function() {
            alert("checkme");
          }
        }
      })
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
      <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.min.js"></script>
      
      <script type="text/x-template" id="modal-template">
        <transition name="modal">
          <div class="vm-modal-mask">
            <div class="vm-modal-wrapper">
              <div class="vm-modal-container">
      
                <div class="vm-modal-header">
                  <slot name="header">
                    default header
                  </slot>
                </div>
      
                <div :class="bgClass" class="vm-modal-body">
                  <slot name="body">
                    default body
                  </slot>
                </div>
      
                <div class="vm-modal-footer">
                  <slot name="footer">
                    &nbsp;
                    <button class="modal-default-button btn btn-primary" @click="clickHandler">
                     Got It!
                      </button>
                  </slot>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </script>
      <div id="app">
        <h5>hello <i style="font-size:20px;cursor:pointer;" aria-hidden="true" class="fa fa-info-circle" v-on:click="showModalZ=true"></i></h5>
        <modal v-if="showModalZ" @close="showModalZ = false">
          <h5 slot="header"><strong>input goes here</strong></h5>
          <hr>
          <div>test</div>
        </modal>
      </div>

      【讨论】:

      • 我的问题是关于触发“清除”方法而不是“关闭”
      • 示例没有这样做
      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2011-07-28
      相关资源
      最近更新 更多