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); }