Vue的watch和computed方法的使用


Vue的watch屬性

Vue的watch屬性可以用來監聽data屬性中數據的變化

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="lib/vue.min.js"></script>
		<script src="lib/vue-router-3.0.1.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model="firstname" />
		</div>
		<script type="text/javascript">
			var vm = new Vue({
				el:"#app",
				data:{
					firstname:"",
					lastname:""
				},
				methods:{},
				watch:{
					firstname:function(){
						console.log(this.firstname)
					}
				}
			})
		</script>
	</body>
</html>

可以從上述代碼中實踐得知,輸入框內的值變化多少次,控制台就會打印多少次

同時還可以直接在監聽的function中使用參數來獲取新值與舊值

				watch:{
					firstname:function(newValue,OldValue){
						console.log(newValue);
						console.log(OldValue);
					}
				}

其中第一個參數是新值,第二個參數是舊值

同時Watch還可以被用來監聽路由router的變化,只是這里的監聽的元素是固定的

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="lib/vue.min.js"></script>
		<script src="lib/vue-router-3.0.1.js"></script>
		<style type="text/css">
		</style>
	</head>
	<body>
		
		<div id="app">
			<!--
            	由於Vue-router的hash匹配原則所以我們需要在原定義的路徑上加一個#號
            -->
<!--			<a href="#/login">登錄</a>
			<a href="#/register">注冊</a>-->
			<router-link to="/login" tag="span">登錄</router-link>
			<router-link to="/register">注冊</router-link>
			<router-view></router-view>
		</div>
	</body>
	<script>
		var login={
			template:'<h1>登錄組件</h1>'
		}
		var register={
			template:'<h1>注冊組件</h1>'
		}
		var routerObj = new VueRouter({
			routes:[
			//此處的component只能使用組件對象,而不能使用注冊的模板的名稱
				{path:"/login",component:login},
				{path:"/register",component:register}
			]
		})
		var vm = new Vue({
			el:'#app',
			data:{
			},
			methods:{
				
			},
			router:routerObj,//將路由規則對象注冊到VM實例上
			watch:{
				'$route.path':function(newValue,OldValue){
						console.log(newValue);
						console.log(OldValue);
				}
			}
		})
	</script>
</html>

計算屬性Computed的作用

computed屬性的作用與watch類似,也可以監聽屬性的變化

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="lib/vue.min.js"></script>
		<script src="lib/vue-router-3.0.1.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model="firstname" />
			<input type="text" v-model="lastname" />
			<input type="text" v-model="fullname" />
		</div>
		<script type="text/javascript">
			var vm = new Vue({
				el:"#app",
				data:{
					firstname:"",
					lastname:""
				},
				methods:{},
/*				watch:{
					firstname:function(newValue,OldValue){
						console.log(newValue);
						console.log(OldValue);
					}
				}*/
				computed:{
					fullname:function(){
						return this.firstname +"-"+this.lastname
					}
				}
			})
		</script>
	</body>
</html>

只是他會根據他依賴的屬性,生成一個屬性,讓vm對象可以使用這個屬性

methods,watch,computed的區別

  1. computed屬性的結果會被緩存,除非依賴的響應式屬性變化才會重新計算。主要當作屬性來使用;
  2. methods方法表示一個具體的操作,主要書寫業務邏輯;
  3. watch一個對象,鍵是需要觀察的表達式,值是對應回調函數。主要用來監聽某些特定數據的變化,從而進行某些具體的業務邏輯操作;可以看作是computedmethods的結合體;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM