Vue.js学习(二):computed响应数据变化与其他几种方法的比较

Vue数据侦听机制
本文介绍了Vue中methods、computed、watch及v-model四种数据侦听方式的特点与应用场景。methods适用于简单计算;computed基于依赖缓存,高效计算;watch用于异步或高开销操作;v-model实现数据双向绑定。

vue重methods、computed、watch以及v-model都能侦听数据,并相应数据的变化

1)methods:每次获取都会重新计算求值

    <div id="app">
        <label for="firstName">First Name:</label>
        <input type="text" id="firstName" v-model="first" @keypress="change">
        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" v-model="last" @keypress="change">

        <h2>methods: {{name}}</h2>
    </div>

let vm=new Vue({
    el:'#app',
    data:{
        price: 1
    },
    methods:{
        change(){
            this.price
        }
    }
})

2)computed:基于数据依赖进行缓存,只有当数据变化时,才会重新求值;

    <div id="app">
        <label for="firstName">First Name:</label>
        <input type="text" id="firstName" v-model="first">
        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" v-model="last">

        <h2>computed: {{cap}}</h2>
    </div>
let vm=new Vue({
    el:'#app',
    data:{
        first:'',
        last: "",
        name:""
    },
    computed:{
        cap:function(){
            return this.first+" "+this.last
        }
    }
})

3)watch:当需要在数据变化时执行异步或者开销较大的操作时,这个方式最有用;

    <div id="app">
        <label for="firstName">First Name:</label>
        <input type="text" id="firstName" v-model="first">
        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" v-model="last">

        <h2>watch: {{cap}}</h2>
    </div>

let vm=new Vue({
    el:'#app',
    data:{
        first:'',
        last: "",
        name:""
    },
    watch:{
        first(value){
            this.name=value+' '+this.last
        },
        last(value){
            this.name=this.first+" "+value
        }
    }
})

4)v-model:这是基于数据双向绑定的

    <div id="app">
        <label for="firstName">First Name:</label>
        <input type="text" id="firstName" v-model="first">
        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" v-model="last">

        <h2>v-model:{{first+' '+last}}</h2> 
    </div>



:8081/#/user:1 Access to XMLHttpRequest at 'http://localhost:8080/user/list' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. xhr.js:175 POST http://localhost:8080/user/list net::ERR_FAILED dispatchXhrRequest @ xhr.js:175 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:52 Promise.then request @ Axios.js:57 Axios.<computed> @ Axios.js:77 wrap @ bind.js:9 getTableData @ User.vue:141 mounted @ User.vue:225 invokeWithErrorHandling @ vue.esm.js:1872 callHook @ vue.esm.js:4244 insert @ vue.esm.js:3167 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2193 invokeInsertHook @ vue.esm.js:6401 patch @ vue.esm.js:6620 Vue._update @ vue.esm.js:3972 updateComponent @ vue.esm.js:4090 get @ vue.esm.js:4504 run @ vue.esm.js:4579 flushSchedulerQueue @ vue.esm.js:4335 eval @ vue.esm.js:1998 flushCallbacks @ vue.esm.js:1924 Promise.then timerFunc @ vue.esm.js:1951 nextTick @ vue.esm.js:2008 queueWatcher @ vue.esm.js:4427 update @ vue.esm.js:4569 notify @ vue.esm.js:739 reactiveSetter @ vue.esm.js:1064 proxySetter @ vue.esm.js:4653 handleItemClick @ element-ui.common.js:3358 invokeWithErrorHandling @ vue.esm.js:1872 Vue.$emit @ vue.esm.js:3912 dispatch @ emitter.js:29 handleClick @ element-ui.common.js:4090 invokeWithErrorHandling @ vue.esm.js:1872 invoker @ vue.esm.js:2197 original._wrapper @ vue.esm.js:7609 createError.js:15 Uncaught (in promise) Error: Network Error at createError (createError.js:15:1) at XMLHttpRequest.handleError (xhr.js:87:1)
05-14
mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 invoker @ vue.runtime.esm.js:5589 mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 invoker @ vue.runtime.esm.js:5589 mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 invoker @ vue.runtime.esm.js:5589 mp.esm.js:481 切换状态: true mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 set @ vue.runtime.esm.js:2642 togglePassword @ login.vue:55 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 setTimeout (async) invoker @ vue.runtime.esm.js:5586 mp.esm.js:481 切换状态: false mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 set @ vue.runtime.esm.js:2642 togglePassword @ login.vue:55 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 setTimeout (async) invoker @ vue.runtime.esm.js:5586 mp.esm.js:481 切换状态: true mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 set @ vue.runtime.esm.js:2642 togglePassword @ login.vue:55 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 setTimeout (async) invoker @ vue.runtime.esm.js:5586 mp.esm.js:481 切换状态: false mp.esm.js:481 [Vue warn]: Property "loginBtnColor" was accessed during render but is not defined on instance. at <App > (anonymous) @ mp.esm.js:481 warn$1 @ vue.runtime.esm.js:1251 get @ vue.runtime.esm.js:2627 _sfc_render @ login.vue:71 renderComponentRoot @ vue.runtime.esm.js:4973 componentUpdateFn @ vue.runtime.esm.js:5100 run @ vue.runtime.esm.js:180 instance.update @ vue.runtime.esm.js:5124 callWithErrorHandling @ vue.runtime.esm.js:1356 flushJobs @ vue.runtime.esm.js:1563 Promise.then (async) queueFlush @ vue.runtime.esm.js:1472 queueJob @ vue.runtime.esm.js:1466 (anonymous) @ vue.runtime.esm.js:5118 resetScheduling @ vue.runtime.esm.js:263 trigger @ vue.runtime.esm.js:403 set @ vue.runtime.esm.js:524 set @ vue.runtime.esm.js:2642 togglePassword @ login.vue:55 (anonymous) @ login.vue:71 callWithErrorHandling @ vue.runtime.esm.js:1356 callWithAsyncErrorHandling @ vue.runtime.esm.js:1363 invoke @ vue.runtime.esm.js:5577 setTimeout (async) invoker @ vue.runtime.esm.js:5586 [worker] reportRealtimeAction:fail not support
08-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值