码迷,mamicode.com
首页 > 其他好文 > 详细

vue2.0 之计算属性和数据监听

时间:2017-06-27 23:25:45      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:inpu   ros   type   ext   soft   return   eth   template   16px   

计算属性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>

技术分享

 

vue2.0 之计算属性和数据监听

标签:inpu   ros   type   ext   soft   return   eth   template   16px   

原文地址:http://www.cnblogs.com/shhnwangjian/p/7087438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!