Vue.js 在最初隐藏的模式中调用方法(转换)
我在我的app/vue中初始化了一个简单的方法“updateTotal”:Vue.js 在最初隐藏的模式中调用方法(转换),vue.js,Vue.js,我在我的app/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部分的HT
// start app
new Vue({
el: '#app2',
data: {
showModal1: false,
showModal2: false,
total : 0
},
methods: {
updateTotal: function (num) {
this.total = this.total + num
}
}
})
当我在app2部分的HTML代码中通过示例按钮调用这个方法时,它是OK的(文本“total”已更新)
当我从加载页面时隐藏的div部分调用此方法时(这是一个模式,使用vue.js“transition”系统),我有以下错误:
属性或方法“updateTotal”未在实例上定义,但在呈现期间被引用。通过初始化属性,确保此属性在数据选项中或对于基于类的组件是被动的
此模式/转换的代码(在app2 div中)为:
标题
是的
不
是的
不
默认页脚
好啊
我能做什么才能从这个模态/转换div调用这个方法
谢谢
MAMP MySQL PHP5由于您试图从其他组件的模板中调用
updateTotal()
,因此出现了此问题
模板中的上下文始终是组件本身(此
)。由于此组件中未定义方法updateTotal()
,因此无法调用它
有几种解决办法,但我认为两个最干净:
对于简单的项目/应用程序:发出一个事件来触发父级中的方法(如close
事件)
对于更复杂的应用程序:与Vuex一起使用共享状态,并使您的方法成为操作
<!-- 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>