vue2.0 之计算属性和数据监听
计算属性computed
<template> <div> <input type="text" name="" v-model="myVal"><br/> {{ myValueWithoutNum }}<br/> {{ getMyValueWithoutNum() }}<br/> </div> </template> <script> export default { data () { return { myVal: '' } }, computed: { myValueWithoutNum () { return this.myVal.replace(/\d/g, '') } }, methods: { getMyValueWithoutNum () { return this.myVal.replace(/\d/g, '') } } } </script> <style> html { height: 100%; } </style>
上例中myValueWithoutNum是计算属性,getMyValueWithoutNum()是方法调用。
数据监听watch
<template> <div> <input type="text" name="" v-model="myVal"><br/> </div> </template> <script> export default { data () { return { myVal: '' } }, watch: { myVal (val, oldval) { console.log(val, oldval) } } } </script> <style> html { height: 100%; } </style>