一 .VUE使用 https://cn.vuejs.org/v2/guide/routing.html
1.Vue成员获取(挂载)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> {{a}} </div> <script> //vm.$el -> 就是元素 var vm=new Vue({ el:'#box', data:{ a:1 } }); //alert(vm.$el); vm.$el.style.background='red'; </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012152831799-5241066.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> {{a}} </div> <script> // vm.$data -> 就是data var vm=new Vue({ el:'#box', data:{ a:1 } }); console.log(vm.$data.a); </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012153045592-1422649219.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> <span v-text="a"></span> </div> <script> // vm.$mount -> 手动挂在vue程序 //angular.bootstrap /*var vm=new Vue({ data:{ a:1 } }); vm.$mount('#box'); //手动挂载*/ var vm=new Vue({ data:{ a:1 } }).$mount('#box'); </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012153314474-1971925295.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> <span v-text="a"></span> <br> {{aa}} </div> <script> //vm.$options -> 获取自定义属性 var vm=new Vue({ aa:114444, //自定义属性, show:function(){ alert(1); }, data:{ a:"vm.$options -> 获取自定义属性" } }).$mount('#box'); vm.$options.show(); console.log(vm.$options.aa); </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012153626987-218101935.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> <span v-text="a"></span> </div> <script> // vm.$log(); -> 查看现在数据的状态 var vm=new Vue({ data:{ a:33333333, b:44444444444 } }).$mount('#box'); console.log(vm.$log()); </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012153827415-2022331617.png)
2. 计算属性computed
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> a => {{a}} <br> b => {{b}} </div> <script> //* computed里面可以放置一些业务逻辑代码,一定记得return var vm=new Vue({ el:'#box', data:{ a:1 }, computed:{ b:function(){ //业务逻辑代码 return 2; } } }); console.log(vm.a); </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012154234993-1899664866.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> a => {{a}} <br> b => {{b}} </div> <script> //* computed里面可以放置一些业务逻辑代码,一定记得return vax', datar vm=new Vue({ el:'#box:{ a:1 }, computed:{ b:function(){ //业务逻辑代码 return this.a+1; } } }); document.onclick=function(){ vm.a=101; }; </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012154406778-1490062423.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> a => {{a}} <br> b => {{b}} </div> <script> //* computed里面可以放置一些业务逻辑代码,一定记得return var vm=new Vue({ el:'#box', data:{ a:1 }, computed:{ b:{ get:function(){ return this.a+2; }, set:function(val){ this.a=val; } } } }); document.onclick=function(){ vm.b=45; }; </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012154521384-590057215.png)
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012154536259-817443867.png)
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8"> <title>Title</title> <!--引入最新的vue稳定版本--> <script src="vue.js"></script> </head> <body> <div id="app"> 总价: {{ prices }} </div> <!--script脚本--> <script> var app = new Vue({ el: '#app', data: { package1: [ { "name":'iPhone7', "price":5000, "count": 1 }, { "name":'ipad', "price":1000, "count": 1 } ], package2: [ { "name":'apple', "price":2000, "count": 2 }, { "name":'banana', "price":2000, "count": 2 } ], }, computed: { prices: function () { //this指向的是当前vue实例对象 //计算总价 var prices=0; //遍历第一个 for (var i=0;i<this.package1.length;i++ ) { prices=prices+this.package1[i].price*this.package1[i].count; } //遍历第二个数组 for (var i=0;i<this.package2.length;i++ ) { prices=prices+this.package2[i].price*this.package2[i].count; } return prices; } } }) </script> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012155524461-202274942.png)
3. $watch 监听数据变化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> window.onload=function(){ var vm=new Vue({ el:'#box', data:{ a:111, b:2 } }); vm.$watch('a',function(){ // alert('发生变化了'); this.b=this.a+100; }); document.onclick=function(){ vm.a=1; }; }; </script> </head> <body> <div id="box"> {{a}} <br> {{b}} </div> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160323173-1963620761.png)
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160337450-837165973.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> window.onload=function(){ var vm=new Vue({ el:'#box', data:{ json:{name:'strive',age:16}, b:2 } }); vm.$watch('json',function(){ alert('发生变化了'); }); document.onclick=function(){ vm.json.name='aaa'; }; }; </script> </head> <body> <div id="box"> {{json | json}} <br> {{b}} </div> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160507150-384961334.png)
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160523594-308235779.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> window.onload=function(){ var vm=new Vue({ el:'#box', data:{ json:{name:'strive',age:16}, b:2 } }); vm.$watch('json',function(){ alert('发生变化了'); },{deep:true}); //{deep:true})深度监视 document.onclick=function(){ vm.json.name='aaa'; }; }; </script> </head> <body> <div id="box"> {{json | json}} <br> {{b}} </div> </body> </html>
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160649783-1382644298.png)
![](https://img2018.cnblogs.com/blog/1760766/201910/1760766-20191012160701097-225467939.png)
4.