当前位置:   article > 正文

vue学习02_vue双花括号表达式

vue双花括号表达式

12、基本语法

动态载入内容{{}},这个双花括号是一种语法,叫做mustache语法,可以将vue实例里面的数据动态显示

  1. 这个就是vue的helloworld
  2. 我们看到了双花括号的语法
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>自己</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>{{message}}</h1>
  12. </div>
  13. <script src="../js/vue.js"></script>
  14. <script>
  15. const app = new Vue({
  16. el: '#app',
  17. data: {
  18. message: '你好',
  19. },
  20. methods: {
  21. },
  22. created :{
  23. },
  24. computed:{
  25. }
  26. });
  27. </script>
  28. </body>
  29. </html>

浏览器上运行如下

 13、插值

v-once |

v-html,v-text

v-pre  |

v-cloak

14、动态绑定属性

v-bind

属性有class等等等

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style>
  7. .red{
  8. color: red;
  9. }
  10. .bbbb{
  11. color: antiquewhite;
  12. }
  13. .div{
  14. width: 500px;
  15. height: 400px;
  16. background-color: tan;
  17. ;
  18. }
  19. .div2{
  20. width: 500px;
  21. height: 200px;
  22. background-color: peachpuff;
  23. ;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="app">
  29. <!--
  30. <h1 :class="{red:true,line:true}">第一名</h1> class绑定了2个,都为true
  31. <h1 :class="{red:true,line:false}">第一名</h1> class绑定了1个,red为true
  32. <button v-on:click="btnclick()">按钮</button> btnclick() 的()可以省略
  33. -->
  34. <h1 :class="{red:flagg}">第一名</h1>
  35. <h1 :class="red">第二名</h1>
  36. <h1 :class="red">第三名</h1>
  37. <h1 :class="getClass()">国家</h1>
  38. <h1 :class="['aaa','bbb']">我爱吃橘子</h1> <!--这里是常量,加了引号-->
  39. <h1 :class="[aaa,bbb]">我爱吃芒果</h1> <!--这里是变量,来自data里面-->
  40. <button v-on:click="btnclick()">按钮</button>
  41. <div :class="div">
  42. <ul>
  43. <li v-for="(item,index) in fuList" v-on:click="clickco(index)" :class="{red:index === type}">{{item+' '+index}}</li>
  44. </ul>
  45. <!--
  46. <h1 :style="{fontSize:'100px'}">style测试</h1>
  47. fontSize 就是 font-size:100px
  48. -->
  49. <h1 :style="{fontSize:'30px'}">style测试1</h1>
  50. <h1 :style="{fontSize:size}">style测试2</h1>
  51. <h2 :style="{color:color}">style测试3</h2>
  52. </div>
  53. <div :class="div2">
  54. <h2 :style="[astyle,bstyle]">五十六个名族</h2>
  55. <h2 v-on:click="getallname()">获取名字{{allname}}</h2>
  56. <!--
  57. {{}}还可以写方法
  58. -->
  59. <h2 >获取名字2{{getallname()}}</h2>
  60. <!--
  61. 用计算属性获取名字
  62. -->
  63. <h2 >获取名字3{{fullname}}</h2>
  64. <!--
  65. 计算这些书的总价格
  66. -->
  67. <h2>{{totalPrice}}</h2>
  68. <h2>{{totalPrice2}}</h2>
  69. <!-- 计算属性的get方法 -->
  70. <h2> 计算属性的get方法--{{fullname2}}</h2>
  71. </div>
  72. </div>
  73. <script src="../js/vue.js"></script>
  74. <script>
  75. const app = new Vue({
  76. el:'#app',
  77. data:{
  78. message:'hello',
  79. tid:1,
  80. red:'red',
  81. flagg:false,
  82. aaa:'haha',
  83. bbb:'bbbb',
  84. div:'div',
  85. fuList:['香蕉','苹果','猕猴桃','西瓜'],
  86. type:-1,
  87. size:'20px',
  88. color:'red',
  89. astyle:{color: 'red'},
  90. bstyle:{fontSize:'20px'},
  91. div2:'div2',
  92. first:'张',
  93. last:'三',
  94. allname:null,
  95. books:[
  96. {id: 110,name: '朝花夕拾',author: '鲁迅',price:11},
  97. {id: 111,name: 'vue学习之路必需学的那些',author: '张丹吉',price:21},
  98. {id: 112,name: '富爸爸穷爸爸',author: '外国人',price:41},
  99. {id: 113,name: '半生缘',author: '张爱玲',price:1},
  100. ],
  101. },
  102. // computed计算属性
  103. //computed和methods里面都是写方法的,但是computed的方法名字一般不带get这种动词,因为它是计算属性
  104. computed:{
  105. fullname: function(){
  106. return this.first+this.last
  107. },
  108. //计算属性里面有get和set方法
  109. fullname2:{
  110. set: function(){
  111. },
  112. get: function(){
  113. return 'aaa'
  114. }
  115. },
  116. totalPrice: function(){
  117. let result = 0
  118. for(let i = 0;i<this.books.length;i++){
  119. result += this.books[i].price
  120. }
  121. return result
  122. },
  123. //下面这种写法更简单
  124. totalPrice2: function(){
  125. let result = 0
  126. for(let book of this.books){
  127. result += book.price
  128. }
  129. return result
  130. }
  131. },
  132. methods:{
  133. btnclick: function(){
  134. this.flagg = !this.flagg
  135. },
  136. getClass: function(){
  137. return {red:true,line:true}
  138. },
  139. clickco: function(index){
  140. console.log("点击了"+index)
  141. this.type=index
  142. },
  143. getallname: function(){
  144. this.allname=this.first+this.last
  145. console.log(this.allname)
  146. return this.first+this.last
  147. }
  148. }
  149. })
  150. </script>
  151. </body>
  152. </html>

v-bind:   =  :

:class就是为元素绑定该class

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <h2>{{message}}</h2>
  10. <h2>{{fullname}}</h2>
  11. <h2>{{getfullname()}}</h2>
  12. </div>
  13. <script src="../js/vue.js"></script>
  14. <script>
  15. const app = new Vue({
  16. el:'#app',
  17. data:{
  18. message:'hello',
  19. firstname:'Kobe',
  20. lastname:'Bryan',
  21. },
  22. methods:{
  23. getfullname: function(){
  24. console.log("getfullname")
  25. return this.firstname+" "+this.lastname
  26. },
  27. },
  28. computed:{
  29. fullname: function(){
  30. return this.firstname+" "+this.lastname
  31. }
  32. },
  33. })
  34. </script>
  35. </body>
  36. </html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <script src="../js/vue.js"></script>
  9. <script>
  10. /*
  11. 注释 Ctrl + shift + /
  12. 定义一个对象
  13. const 是常量,指向的对象不可改变,但是里面属性的值可以改
  14. */
  15. /* const obj ={
  16. name:'张三',
  17. age:'18',
  18. sex:'男'
  19. }
  20. console.log(obj) */
  21. const name ='王五';
  22. /* 注意这里要用分号;
  23. ES5的写法
  24. */
  25. const age ='18';
  26. const obj ={
  27. username:name,
  28. age:age
  29. }
  30. console.log("1",obj)
  31. const wayes5 = {
  32. run : function (){
  33. },
  34. eat: function(){
  35. },
  36. }
  37. console.log(wayes5)
  38. /* 注意这里要用分号;
  39. ES6的写法
  40. */
  41. const username = '小燕子'
  42. const obj2 ={
  43. username,
  44. age,
  45. }
  46. console.log("2",obj2)
  47. /*
  48. 函数的增强写法
  49. */
  50. const wayes6 = {
  51. run(){
  52. console.log("这里是es6的写法")
  53. }
  54. };
  55. console.log(wayes6)
  56. </script>
  57. </body>
  58. </html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <h2>{{message}}</h2>
  10. <!-- <button v-on:click="message++">+</button>
  11. <button v-on:click="message--">-</button> -->
  12. <!--
  13. ctrl + / 块注释
  14. -->
  15. <button v-on:click="add">+</button>
  16. <button v-on:click="sub">-</button>
  17. <h2>v-on没有参数</h2>
  18. <!-- 没有参数的时候()可以写可以不写 -->
  19. <button @click="add()">按钮1</button>
  20. <button @click="sub">按钮2</button>
  21. <h2>v-on参数</h2>
  22. <button @click="fly(1)">按钮1</button>
  23. <button @click="show1">按钮2</button>
  24. <button @click="show(123,$event)">按钮3</button>
  25. <button @click="show(name,$event)">按钮4</button>
  26. <!--
  27. 点击按钮,两个都会触发,冒泡
  28. 阻止冒泡 @click.stop
  29. 阻止默认事件 .prevent
  30. -->
  31. <div @click="divClick()">
  32. <button @click.stop="btnClick()">按钮</button>
  33. <br />
  34. <form action="baidu">
  35. <input type="submit" value="sumbit" @click.prevent="sumbitc"/>
  36. <!-- 监听键盘松开的事件 -->
  37. <input type="text" @keyup="keyUp()" />
  38. <input type="text" @keyup.enter="keyUp()" />
  39. </form>
  40. </div>
  41. </div>
  42. <script src="../js/vue.js"></script>
  43. <script>
  44. const app = new Vue({
  45. el:'#app',
  46. data:{
  47. message:0,
  48. name:'张三'
  49. },
  50. // methods里面的方法用,逗号隔开
  51. methods: {
  52. add(){
  53. this.message++
  54. },
  55. sub(){
  56. this.message--
  57. },
  58. fly(a){
  59. console.log("1------------")
  60. },
  61. show1(event){
  62. console.log(event)
  63. },
  64. show(a,event){
  65. console.log(a,event)
  66. },
  67. divClick(){
  68. console.log("divClick")
  69. },
  70. btnClick(){
  71. console.log("btnClick")
  72. },
  73. sumbitc(){
  74. console.log("提交")
  75. },
  76. keyUp(){
  77. console.log("v")
  78. }
  79. }
  80. });
  81. </script>
  82. </body>
  83. </html>

事件监听

v-on

v-on:click  =  @click  为元素绑定点击的监听函数

v-bind :class = :class

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/174117
推荐阅读
相关标签