1.new Vue 创建一个实例,传入一个对象。
2.对象包括:
el:作用域
data:所用到的数据
methods:所用到的函数
3.{{}} 数据绑定 其中不止可以绑定data,也可以绑定函数(如果这个函数有返回值并且返回值是字符串之类的可以输出的东西)
4.{{}}大括号只能绑定内容,不能在html属性里使用,如:< a href={{}}} >,这是不行的
5.上面那个可以使用 v-bind:href=”link” –> 属性值的绑定,告诉html : 后面的数据是绑定的。
6.使用v-html绑定html标签而不是直接输出字符串,不过这样会造成跨站脚本攻击,不安全。

clipboard.png

html部分:

  1. <div id="app">
  2. 起始值<input v-model="start" />
  3. 步长<input v-model="step" />
  4. <button v-on:click="increase">增加</button>
  5. <button v-on:click="decrease">减少</button>
  6. <br />
  7. <hr />
  8. <span>输出结果:{{result}} </span>
  9. </div>

js部分

  1. <script>
  2. new Vue({
  3. el:'#app',
  4. data:{
  5. start:0,
  6. step:0,
  7. result:0,
  8. },
  9. methods:{
  10. increase:function(){
  11. if(this.result==0){
  12. this.result=parseInt(this.start);
  13. this.result+=parseInt(this.step);
  14. }else{
  15. this.result+=parseInt(this.step);
  16. }
  17. },
  18. decrease:function(){
  19. if(this.result==0){
  20. this.result=parseInt(this.start);
  21. this.result-=parseInt(this.step);
  22. }else{
  23. this.result-=parseInt(this.step);
  24. }
  25. },
  26. }
  27. })
  28. </script>

这个例子用到了:
1.v-model input框的双向绑定。
2.v-on:click 监听click事件,其他事件道理类似。

  1. <div id="app">
  2. 起始值<input v-model="start" />
  3. 步长<input v-model="step" />
  4. <button v-on:click="increase">增加</button>
  5. <button v-on:click="decrease">减少</button>
  6. <button v-on:click="increase2">增加2</button>
  7. <br />
  8. <hr />
  9. <span>输出结果:{{resultPrint()}} </span>
  10. <br />
  11. <span>sum2 is {{sum2}}</span>
  12. </div>
  1. <script>
  2. new Vue({
  3. el:'#app',
  4. data:{
  5. sum2:0,
  6. start:0,
  7. step:0,
  8. result:0,
  9. },
  10. methods:{
  11. increase:function(){
  12. if(this.result==0){
  13. this.result=parseInt(this.start);
  14. this.result+=parseInt(this.step);
  15. }else{
  16. this.result+=parseInt(this.step);
  17. }
  18. },
  19. decrease:function(){
  20. if(this.result==0){
  21. this.result=parseInt(this.start);
  22. this.result-=parseInt(this.step);
  23. }else{
  24. this.result-=parseInt(this.step);
  25. }
  26. },
  27. increase2:function(){
  28. this.sum2++;
  29. },
  30. resultPrint:function(){
  31. console.log("resultPrint的打印")
  32. return this.result>10? '结果大于10':'结果小于10'
  33. }
  34. }
  35. })
  36. </script>

解析:如果resultPrint是一个函数的话,不管我点击按钮1还是按钮2,由于并不知道按钮2是否会影响到resultPrint的输出值,因此无论页面做什么操作,resultPrint都会渲染重新执行,如果resultPrint是一个计蒜属性的话,既可以解决普通属性无法加逻辑的局限,又可以避免写成一个函数的话不必要的执行。

  1. computed:{
  2. resultPrint:function(){
  3. console.log("resultPrint的打印")
  4. return this.result>10? '结果大于10':'结果小于10'
  5. }
  6. },
  1. <div id="app">
  2. <img v-bind:src="picUrl" v-on:click="changPic" />
  3. </div>
  1. <script>
  2. new Vue({
  3. el:'#app',
  4. data:{
  5. index:0,
  6. sum2:0,
  7. start:0,
  8. step:0,
  9. result:0,
  10. picUrl:'https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1560069366&di=8b211d63775a606bf33b3a362b2b475d&src=http://hbimg.b0.upaiyun.com/54ebececeda0217648263cc944d6cfd413a17cdf2cc6-MGHS0y_fw658'
  11. },
  12. methods:{
  13. changPic:function(){
  14. if(this.index==0){
  15. this.picUrl='https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1130583636,2033493811&fm=26&gp=0.jpg'
  16. this.index=1;
  17. }else{
  18. this.picUrl='https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1560069366&di=8b211d63775a606bf33b3a362b2b475d&src=http://hbimg.b0.upaiyun.com/54ebececeda0217648263cc944d6cfd413a17cdf2cc6-MGHS0y_fw658'
  19. this.index=0;
  20. }
  21. }
  22. }
  23. })
  24. </script>

版权声明:本文为CszShuzi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/CszShuzi/p/11056874.html