简体   繁体   English

Vue在模态渲染模板中调用function

[英]Vue calling function in rendered template for modal

I have a modal that pops up with checkboxes that seem to succesfully be adding the value to my two data props when checked.我有一个弹出复选框的模式,这些复选框似乎在选中时成功地将值添加到我的两个数据道具中。 However, upon submitting I get an error on the called function.但是,在提交时,我在名为 function 上收到错误消息。

I think this is becuase the button calling the function is in a rendered modal template and it doesn't see global function.我认为这是因为调用 function 的按钮位于呈现的模态模板中,并且看不到全局 function。

How can I fix this so that hitting 'Save' will properly call saveDetails so that I can gather my form info to send via axios?我该如何解决这个问题,以便点击“保存”将正确调用saveDetails ,以便我可以收集我的表单信息以通过 axios 发送?

new Vue({
el:'#app',
data: { 

    typeNames: [],
    siteNames: []
},
methods: {

    saveDetails: function(event){
        console.log(this.siteNames);
        console.log(this.typeNames); 
    }
}


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

          <div class="modal_context uk-form-row uk-width-1-2">
            <slot name="site">
                <input type="checkbox" name='customeradded' value='customer'  v-modal="siteNames" data-md-icheck  />
                <label>Customer</label><br>
                <input type="checkbox" name='InternalSiteadded' value='InternalSite' v-modal="siteNames" data-md-icheck  />
                <label>InternalSite</label><br>
                <input type="checkbox" name='mixadded' value='mix' v-modal="siteNames" data-md-icheck  />
                <label>mix</label><br>
            </slot>
          </div>

          <div class="modal_type uk-form-row uk-width-1-2">
            <slot name="type">
                <input type="checkbox" name='marketingadded' value='marketing' v-modal="typeNames" data-md-icheck  />
                <label>marketing</label><br>
                <input type="checkbox" name='catalogadded' value='catalog' v-modal="typeNames" data-md-icheck  />
                <label>catalog</label><br>
            </slot>
          </div>

          <div class="modal-footer uk-form-row uk-width-1-1">
            <slot name="footer">
              <button class="modal-default-button" @click="$emit('close')">
                Cancel
              </button>
              <button class="modal-save-button" @click="$emit('close'); saveDetails();">
                Save
              </button>
            </slot>
          </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>

The easiest way would be to call a handler function.最简单的方法是调用处理程序 function。 That function will be responsible of executing two lines of code, emit close event and call saveDetails function with the original event. function 将负责执行两行代码,发出关闭事件并使用原始事件调用 saveDetails function。 You can do it in line or placing that handler in the methods.您可以在线执行或将该处理程序放在方法中。 My personal preference is to avoid any logic on the template, just reference methods on it.我个人的偏好是避免模板上的任何逻辑,只是引用它上面的方法。 Code will look like as follows.代码如下所示。

new Vue({
el:'#app',
data: { 
    typeNames: [],
    siteNames: []
},
methods: {
    saveHandler(event) {
        this.$emit('close');
        this.saveDetails(event);
    },
    saveDetails: function(event){
        console.log(this.siteNames);
        console.log(this.typeNames); 
    }
}


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

          <div class="modal_context uk-form-row uk-width-1-2">
            <slot name="site">
                <input type="checkbox" name='customeradded' value='customer'  v-modal="siteNames" data-md-icheck  />
                <label>Customer</label><br>
                <input type="checkbox" name='InternalSiteadded' value='InternalSite' v-modal="siteNames" data-md-icheck  />
                <label>InternalSite</label><br>
                <input type="checkbox" name='mixadded' value='mix' v-modal="siteNames" data-md-icheck  />
                <label>mix</label><br>
            </slot>
          </div>

          <div class="modal_type uk-form-row uk-width-1-2">
            <slot name="type">
                <input type="checkbox" name='marketingadded' value='marketing' v-modal="typeNames" data-md-icheck  />
                <label>marketing</label><br>
                <input type="checkbox" name='catalogadded' value='catalog' v-modal="typeNames" data-md-icheck  />
                <label>catalog</label><br>
            </slot>
          </div>

          <div class="modal-footer uk-form-row uk-width-1-1">
            <slot name="footer">
              <button class="modal-default-button" @click="$emit('close')">
                Cancel
              </button>
              <button class="modal-save-button" @click="saveHandler">
                Save
              </button>
            </slot>
          </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>

Despite this.尽管如此。 I can see your code in incomplete and this will not work, because your Vue instance is not bind to that template.我可以看到您的代码不完整,这将不起作用,因为您的 Vue 实例未绑定到该模板。 It does not exist such node with id="app" .它不存在id="app"的此类节点。 Looks like you want to emit close but I don't understand who is the parent component listening to that close event.看起来你想发出close ,但我不明白谁是监听该close事件的父组件。

Define it as a component and trigger event with data you want to pass when a user clicks.将其定义为一个组件,并在用户单击时使用要传递的数据触发事件。 https://v2.vuejs.org/v2/guide/components.html https://v2.vuejs.org/v2/guide/components.html

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

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