vue 语法

插值绑定

插值绑定是Vue中最常见、最基本的语法,绑定的内容主要有文本插值HTML插值两种。

文本插值

文本插值的方式十分简单,只要用双大括号(Mustache语法)将要绑定的变量、值、表达式括住就可以实现,Vue将会获取计算后的值,并以文本的形式将其展示出来。

<template>
   <div>
       <p>
           <label class="pro file"> 变量: </label>{{num}}
       </p>
       <p>
           <label class="pro file"> 计算: </label>{{num + 5}}
       </p>
       <p>
           <label class="pro file"> 三目运算符: </label>{{num !== 5 ? 'A' : 'B'}}
       </p>
       <p>
           <label class="pro file"> 匿名函数: </label>{{ (()=>num + 5)() }}
       </p>
       <p>
           <label class="pro file"> 对象: </label>{{ {num1:16} }}
       </p>
       <p>
           <label class="pro file"> 函数对象: </label>{{ getNum }}
       </p>
       <p>
           <label class="pro file"> html代码(表达式): </label>{{ '<span>15</span>' }}
       </p>
       <p>
           <label class="pro file"> html代码(变量): </label>{{ html }}
       </p>
   </div>
</template>

<script>
   export default {
       name: "InsertValue",
       data() {
           return {
               num: 15,
               html: '<span>15</span>'
          }
      },
       methods: {

           getNum() {
               return this.num
          }
      }
  }
</script>

<style scoped>
   .pro file {
       display: inline-block;
       width: 300px;
  }
</style>


 

 

 

可以看到,无论是变量、表达式、执行函数还是DOM代码,Vue都只将结果当作文本处理。另外,如果插值绑定的内容是变量或与变量有关,当变量的值改变时,视图也会同步更新。

 

HTML插值

HTML插值可以动态渲染DOM节点,常用于处理开发者无可预知和难以控制的DOM结构,如渲染用户随意书写的文档结构等,这在一些论坛和博客平台上可以看到,下面来看一段相关代码:

<template>
   <div>
       <div v-html="html"></div>
   </div>
</template>

<script>
   export default {
       name: "InnerHtml",
       data() {
           return {
               html: '<div id="main-message" jstcache="0">\n' +
                   '       ... </div>\n' +
                   '     </div>'
          };
      }
  }
</script>

<style scoped>

</style>

 

 

 

属性绑定

v-bind

<template>
   <div id="app" >
       <p  v-bind:class="className" v-bind:title="title">title</p>
       <p  v-bind:class="className" :title="title">title</p>
       <button  class="className" v-bind:disabled="0 === 1 - 1"></button>
       <button class="className" :disabled="0 === 1 - 1"></button>
       <input  v-bind:placeholder="title === 'title' ? '请输入' : '不要输入'">
       <input :placeholder="title === 'title' ? '请输入' : '不要输入'">
   </div>
</template>

<script>
   export default {
       name: "v-bind",
       data() {
           return {
               className: 'className',
               title: 'title'
          }
      }
  }
</script>

<style scoped>

   .className {
       background-color: #42b983;
       /*width: 5vw;*/
       height: 5vh;
  }
</style>

 

 

 

类名和样式绑定

由于类名class和样式style在节点属性中是两个比较奇怪的存在(虽然他们可接收的类型都是字符串,但类名实际上是由数组拼接而成,而样式则是由对象键值对拼接而成的),所以Vue在绑定类名和样式时也采用不一样的机制。我们可以通过字符串、数组和对象三种方式为节点动态绑定类名属性,代码如下:

<template>
   <div id="app">
       <p v-bind:class="className" v-bind:title="title">title</p>
       <p v-bind:class="className" :title="title">title</p>
       <button class="className" v-bind:disabled="0 === 1 - 1"></button>
       <button class="className" :disabled="0 === 1 - 1"></button>
       <input v-bind:placeholder="title === 'title' ? '请输入' : '不要输入'">
       <input :placeholder="title === 'title' ? '请输入' : '不要输入'">

       <div></div>
       <br>
       <div>
           <label>class样式绑定</label>
           <div v-bind:class="red_h5_w5"> red_h5_w5 </div>
           <div v-bind:class="yellow_h5"> yellow_h5</div>
           <div v-bind:class="yellow_w5"> yellow_w5</div>
       </div>

   </div>
</template>

<script>
   export default {
       name: "v-bind",
       data() {
           return {
               className: 'className',
               title: 'title',
               yellow_w5: {
                   c_yellow: true,
                   w5: true
              },
               yellow_h5: ['c_yellow', 'w5'],
               red_h5_w5: 'c_red h5 w5'
          }
      }
  }
</script>

<style scoped>

   .className {
       background-color: #42b983;
       /*width: 5vw;*/
  }

   .c_yellow {
       background-color: #ffe600;
       /*width: 5vw;*/
  }

   .c_red {
       background-color: #ff2600;
       /*width: 5vw;*/
       /*height: 5vh;*/
  }

   .h5 {
       /*width: 5vw;*/
       height: 5vh;
  }

   .w5 {
       /*width: 5vw;*/
       width: 5vw;
  }
</style>


 

 

 

事件绑定

Vue使用v-on指令监听DOM事件,开发者可以将事件代码通过v-on指令绑定到DOM节点上,基本使用方法如下:

<template>
   <div id="app">

       <button v-on:click="logInfo()"> v-on:click 打印默认消息</button>
       <button v-on:click="logInfo('hahaha')"> v-on:click 打印 hahaha</button>
       <button @click="logInfo('hahaha')"> @click 打印 hahaha</button>
       <button @click="getEvent"> @click= 打印 获取事件对象</button>
       <input type="text" @keyup="getEvent($event)"> @keyup 获取事件对象
   </div>
</template>

<script>
   export default {
       name: "Listener",
       methods: {

           logInfo(msg) {
               console.log(msg || 'hello word')
          },
           getEvent(event) {
               console.log(event)
          }
      }
  }
</script>

<style scoped>

</style>

 

 

 

 

 

常见修饰符

event.preventDefault()(阻止节点默认行为)和event.stopPropagation()(阻止事件冒泡),这是处理DOM事件时很常见的方法,Vue将其封装成简短易用的事件修饰符,可以后缀于事件名称之后。

 

<template>
   <div>

       <label @click="iAmYourFather"> 没有stop修饰符 两个事件都触发
           <button class="w5-h5" @click="iAmYourGrandpa">我是你爷爷</button>
       </label>
       <br>
       <label @click="iAmYourFather"> stop修饰符 只触发iAmYourGrandpa
           <button class="w5-h5" @click.stop="iAmYourGrandpa">我是你爷爷</button>
       </label>
       <hr>
       <form @submit="submitCount"> 刷新页面 count一直为输出1
           <button class="w5-h5" type="submit">提交</button>
       </form>
       <form @submit.prevent="submitCount"> prevent 阻止元素默认方法 阻止刷新
           <button class="w5-h5" type="submit">提交</button>
       </form>
       <hr>
       <label @click.capture="iAmYourFather"> 爸爸先执行
           <button class="w5-h5" @click="iAmYourGrandpa">我是你爷爷</button>
       </label>
       <br>
       <label @click.s="iAmYourFather"> 爷爷我先来
           <button class="w5-h5" @click.capture="iAmYourGrandpa">我是你爷爷</button>
       </label>
       <hr>
       <label @click.self="iAmYourFather"> self 只能响应 我是你爷爷
           <button class="w5-h5" @click.self="iAmYourGrandpa">我是你爷爷</button>
       </label>
       <br>

       <hr>
       <label > 在怎么点 你也只有一个爷爷
           <button class="w5-h5" @click.once="iAmYourGrandpa">爷爷</button>
       </label>
       <br>

   </div>
</template>

<script>
   export default {
       name: "Modifier",
       data() {
           return {
               count: 0
          }
      },
       methods: {

           iAmYourFather() {
               console.log('我是你爸爸')
          },
           iAmYourGrandpa() {
               console.log('我是你爷爷')
          }, submitCount() {
               console.log('count : ' + ++this.count)
          }
      }
  }
</script>

<style scoped>
   .w5-h5 {
       width: 10vw;
       height: 5vh;
  }
</style>

按键修饰符

对于键盘事件,Vue允许将按键键值作为修饰符来使用,如监听回车键(键值13)是否被按下,可以这么写

 

 

 

 

 

 

 

<template>
   <div id="app">

       <h1> 按钮修饰符 </h1>
       <input type="text" @keyup.enter="clickEnter">
   </div>
</template>

<script>
   export default {
       name: "btnModifier",
       methods: {
           clickEnter() {
               alert('兄弟 确定你是1吗')
          }
      }
  }
</script>

<style scoped>

</style>

 

 

 

双向绑定

v-model

v-model和v-show是Vue核心功能中内置的、开发者不可自定义的指令。 我们可以使用v-model为可输入元素(input & textarea)创建双向数据绑定,它会根据元素类型自动选取正确的方法来更新元素。

 

 

 

<template>
   <div id="app">
       <h3>单行文本框</h3>
       <input type="text" v-model="singleText">
       <input type="text" v-model.lazy="singleText">
       <input type="text" v-model.number="singleText">
       <input type="text" v-model.trim="singleText">
       <p>{{singleText}}</p>
       <h3>多行文本框</h3>
       <textarea v-model="multiText"></textarea>
       <pre>{{multiText}}</pre>
       <h3>单选</h3>
       <input id="haha" type="radio" value="大桥" v-model="singleRadio">
       <label> 大桥 </label>
       <input id="hahb" type="radio" value="小乔" v-model="singleRadio">
       <label> 小乔 </label>
       <pre>{{singleRadio}}</pre>
       <h3>单个复选框</h3>
       <input type="checkbox" id="love_xiao_qiao" v-model="switchValue">
       <label>喜欢小乔,{{switchValue? '这都被你发现' : '怎么可能'}}</label>
       <h3>哪一个是你老婆</h3>
       <input id="daqiao" type="checkbox" value="大桥" v-model="checkBoxValue">
       <label> 大桥 </label>
       <input id="xiaoqiao" type="checkbox" value="小乔" v-model="checkBoxValue">
       <label> 小乔 </label>
       <p v-if="checkBoxValue.length>0">{{checkBoxValue.length ===2 ? '小孩子才做选择,我全都':'钟爱一人'}}</p>
       <p>{{checkBoxValue.join(',')}}</p>

       <h3>选老婆</h3>
       <select multiple v-model="multiSelect">
           <option value="大桥">大桥</option>
           <option value="小乔">小乔</option>
           <option value="不明人">不明人</option>
       </select>
       <p>{{multiSelect.join(',')}}</p>

   </div>
</template>

<script>
   export default {
       name: "bothwayBind",
       data() {
           return {
               singleText: '',
               multiText: '',
               singleRadio: '',
               switchValue: true,
               checkBoxValue: [],
               multiSelect: [1,3]
          }
      }

  }
</script>

<style scoped>

</style>

 

 

 

v-model与自定义组件

待学习

 

条件渲染和列表渲染

程序中有三大结构:顺序结构、分支结构、循环机构。顺序结构不必多说,分支和循环结构则分别由条件判断语句和循环语句实现。同样地,Vue也为视图渲染提供了条件判断和循环的机制,简称为条件渲染和列表渲染。

指令v-if和v-show

v-if的使用方法并不复杂,只需要为元素挂上v-if指令即可,与之配套的还有v-else-if和v-else,不过它们只能与v-if配合使用。

v-show也可以用于实现条件渲染,不过它只是简单地切换元素的**CSS属性:display。当条件判定为假时,元素的display属性将被赋值为none;反之,元素的display属性将被恢复为原有值。

v-if会在切换中将组件上的事件监听器和子组件销毁和重建。当组件被销毁时,它将无法被任何方式获取,因为它已不存在于DOM中

●在创建父组件时,如果子组件的v-if被判定为假,Vue不会对子组件做任何事情,直到第一次判定为真时。这在使用Vue生命周期钩子函数时要尤为注意,如果生命周期已走过组件创建的阶段,却仍无法获取组件对象,想一想,是不是v-if在作怪。

v-show有更高的初始渲染开销,而v-if有更高的切换开销,这与它们的实现机制有关,在使用时要多加考虑具体的应用场景。

●v-show不支持template元素,不过在Vue 2.0中,template的应用并不广泛,了解即可。

<template>
   <div id="app">

       <p v-if="0 === condition1">我是0</p>
       <p v-else-if="1 === condition1">我是1</p>
       <p v-else>我是其他</p>
       <p v-show="0 === condition1">我是0</p>
       <p v-show="1 === condition1">我是1,我刚才只是隐藏了</p>
       <p v-show="1 < condition1">我是其他,我刚才只是隐藏了</p>
       <button @click="switchOther">切换</button>
   </div>
</template>

<script>
   export default {
       name: "condition",
       data() {
           return {
               condition1: 0
          }
      },
       methods : {
           switchOther() {
               this.condition1++;
          }
      }
  }
</script>

<style scoped>

</style>

指令v-for

4.0需要key属性

<template>
   <div id="app">

       <ul>
           <li v-for="(user1,index) in list" :key="'f1' + index">
                   <p>姓名:{{user1.name}}</p>
                   <p>性别:{{user1.sex}}</p>
                   <p>年龄:{{user1.age}}</p>
           </li>
           <p> 对象键值对循环</p>
           <li v-for="(user,index) in list" :key="'f2' + index">
               <p>用户{{index}}</p>
               <ul>
                   <li v-for="(value,key) in user" :key="index + key">
                       <p>{{key}} : {{value}}</p>
                   </li>
               </ul>
           </li>
       </ul>



   </div>
</template>

<script>
   export default {
       name: "cycle",
       data() {
           return {
               list: [
                  {
                       name: 'gylang',
                       sex: '男',
                       age: '22'
                  }, {
                       name: 'guoylang',
                       sex: '男',
                       age: '21'
                  }
              ]
          }
      }
  }
</script>

<style scoped>

</style>


 

 

 

 

 

对数据的操作

要注意的是,直接使用下标/键名为数组/对象设置成员时,有以下用法:[插图]Vue并不会将其加入数据响应式系统,因此当数据被修改时,视图不会进行更新。

 

 

 

<template>
   <div id="app">
       <ul>
           <li v-for="(user1,index) in list" :key="'f1' + index">
               <p>姓名:{{user1.name}}</p>
               <p>性别:{{user1.sex}}</p>
               <p>年龄:{{user1.age}}</p>
           </li>
       </ul>
       <form @submit.prevent="create()" >
           <p>姓名:
               <input type="text" v-model="createUser.name">
           </p>
           <input type="radio" v-model="createUser.sex" value="男">
           <label>男</label>
           <input type="radio" v-model="createUser.sex" value="女">
           <label>女</label>
           <p>年龄:
               <input type="text" v-model.number="createUser.age">
           </p>
           <button type="submit">添加</button>
           <button @click.prevent="reserve()">反序</button>
       </form>

   </div>
</template>

<script>
   export default {
       name: "cycle",
       data() {
           return {
               createUser: {
                   name: '',
                   sex: '',
                   age: 0
              },
               list: [
                  {
                       name: 'gylang',
                       sex: '男',
                       age: '22'
                  }, {
                       name: 'guoylang',
                       sex: '男',
                       age: '21'
                  }
              ]
          }
      },
       methods: {
           create() {
               this.list.push(
                  {
                       name: this.createUser.name,
                       sex: this.createUser.sex,
                       age: this.createUser.age
                  }
              )
          },
           reserve() {
               this.list.reverse()
          }
      }
  }
</script>

<style scoped>

</style>


 

 

 

 

posted on 2020-06-02 21:38  gylang  阅读(15)  评论(0编辑  收藏  举报