【问题标题】:Calling a method in a modal initially hidden (transition)在最初隐藏的模式中调用方法(转换)
【发布时间】:2020-01-09 00:30:12
【问题描述】:

我在我的应用程序/vue 中初始化了一个简单的方法“updateTotal”:

// start app
new Vue({
el: '#app2',
data: {
    showModal1: false,
    showModal2: false,
    total : 0
},
methods: {
    updateTotal: function (num) {
      this.total = this.total + num
    }
}
})

当我从 app2 部分的 HTML 代码中的一个按钮示例调用此方法时,一切正常(文本“total”已更新)。

当我从页面加载时隐藏的 div 部分调用此方法时(它是一个模式,带有 vue.js“过渡”系统),我有这个错误: 属性或方法“updateTotal”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。

这个模态/转换的代码(它在 app2 div 中)是:

<!-- template for the modal component SERVICES-->
                <script type="text/x-template" id="modal2-template">
                <transition name="modal2">
                    <div class="modal-mask">
                    <div class="modal-wrapper">
                        <div class="modal-container">

                        <div class="modal-header">
                            <slot name="header">
                            Header
                            </slot>
                        </div>

                        <div class="modal-body">

                                <!-- LISTE SERVICES -->
                                <div>
                                    <div>

                                        <div class="control">
                                        <label class="radio">
                                            <input type="radio" name="service1" v-on:click="updateTotal(10)">
                                            Oui
                                        </label>
                                        <label class="radio">
                                            <input type="radio" name="service1" checked v-on:click="updateTotal(-10)">
                                            Non
                                        </label>
                                        </div>
                                    </div>
                                    <div>


                                        <div class="control">
                                        <label class="radio">
                                            <input type="radio" name="service2" v-on:click="updateTotal(20)">
                                            Oui
                                        </label>
                                        <label class="radio">
                                            <input type="radio" name="service2" checked v-on:click="updateTotal(-20)">
                                            Non
                                        </label>
                                        </div>
                                    </div>
                                </div>
                                <!-- LISTE SERVICES -->

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

我该怎么做才能从这个模态/过渡 div 调用这个方法?

谢谢!

MAMP MySQL PHP5

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    由于您尝试从不同组件的模板中调用 updateTotal(),因此出现此问题。

    template 中的上下文始终是组件本身 (this)。由于该组件中没有定义方法updateTotal(),所以不能调用。

    有几种解决方法,但我认为两种最干净:

    1. 对于简单的项目/应用程序:发出触发父级方法的事件(如您的 close 事件)

    2. 对于更复杂的应用程序:使用与 Vuex 共享状态并将您的方法设为 action

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2014-01-01
      • 2015-05-10
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多