我在我的应用程序/ 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代码中的按钮调用此方法时,就可以了(文本“总计”已更新)。
当我从页面加载时隐藏的div部分调用此方法时(这是一个模态,使用vue.js“转换”系统),我遇到此错误: 属性或方法“ updateTotal”未在实例上定义,但在渲染期间被引用。通过初始化属性,确保该属性在data选项中或对于基于类的组件都是反应性的。
此模式/转换(在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
答案 0 :(得分:0)
由于您尝试从其他组件的模板中调用updateTotal()
,因此出现了问题。
template
中的上下文始终是组件本身(this
)。由于该组件中未定义方法updateTotal()
,因此无法调用它。
有几种解决方法,但我认为其中两种方法最干净:
对于简单的项目/应用程序:发出一个事件来触发父方法(例如您的close
事件)
对于更复杂的应用程序:与Vuex一起使用共享状态并将您的方法设置为action