html代码:
<input type="text" name='age' value='' v-model.number='age'>
script代码:
<script> let app = new Vue({ el: '#app', data: { }, watch: { age: function(val) { console.log(val); } } }) </script>
报错如标题
[Vue warn]: Property or method “Name” is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
查看官方网址:
由于Vue不允许动态添加根级别的响应属性,因此必须通过预先声明所有根级别的响应数据属性来初始化Vue实例,即使是空值
应该写为:
data: { age: "" }, watch: { age: function(val) { console.log(val); }