Vue学习笔记(一)

Vue.js

1、如何引用Vue

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /> -->
    <title>Document</title>
    <!-- 开发版本 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
  
<div id="app">
  {{ message }}
</div>
 
<script>
  var app = new Vue({
    el: '#app',
    data: {
      message: 'Hello Vue!'
    }
  });
</script>
</html>

输出:Hello Vue!

出现问题:
①、运行输出{{ message }}
问题原因:Vue链接有问题,导致没有引用成功
②、拼写Vue时候没有代码提示
使用 newV 这样会有提示

2、Vue的用法

2.1 条件渲染

v-ifv-else

<!DOCTYPE html>
<html lang="en">
  <head>
   
    <title>Document</title>
    <!-- 开发版本 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
  
<div id="app">
  <h1 v-if="some">这是some为true时候展示的内容</h1>
  <h1 v-else>这是some为false时候展示的内容</h1>
</div>
 
<script>
  var app = new Vue({
    el:'#app',
    data:{
      some:false
    },
    
  });
</script>
</html>

输出:这是some为false时候展示的内容

v-else-if(2.1.0新增)

<!DOCTYPE html>
<html lang="en">
  <head>
   
    <title>Document</title>
    <!-- 开发版本 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
  
<div id="app">
 <h1 v-if="type === 'A'">A</h1>
 <h1 v-else-if="type === 'B'">B</h1>
 <h1 v-else-if="type === 'C'">C</h1>
 <h1 v-else>Not ABC</h1>
</div>
 
<script>
  var app = new Vue({
    el:'#app',
    data:{
      type:'A'
    },
    
  });
</script>
</html>

2.2 列表渲染:v-for

  • v-for
<!DOCTYPE html>
<html lang="en">
  <head>
   
    <title>Document</title>
    <!-- 开发版本 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
  
  <ul id="some">
    <li v-for="item in items">
      {{ item.message}}
    </li>
  </ul>
 
 
<script>
  var app=new Vue({
    el:'#some',
    data:{
      items:[
      {  message:'A'},{message:'B'}
      ]

    }
  })
  
</script>
</html>

输出:
在这里插入图片描述

  • v-for 迭代对象
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!--axios  网络请求-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <li v-for="item in objets">{{item}}</li>
      </ul>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          objets: {
            name: "百度",
            url: "http://www.baidu.com",
            slogan: "学的不仅是技术,更是梦想!",
          },
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

  • 也可提供多个参数为键名: v-for="(value, key) in object" 或者 v-for="(value, key, index) in object"

两个参数:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!--axios  网络请求-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <li v-for="(value, key) in objets">{{ index }}. {{ key }}</li>
      </ul>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          objets: {
            name: "百度",
            url: "http://www.baidu.com",
            slogan: "学的不仅是技术,更是梦想!",
          },
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

三个参数:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!--axios  网络请求-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <li v-for="(value, key,index) in objets">
          {{ index }}. {{ key }} : {{ value }}
        </li>
      </ul>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          objets: {
            name: "百度",
            url: "http://www.baidu.com",
            slogan: "学的不仅是技术,更是梦想!",
          },
        },
      });
    </script>
  </body>
</html>

  • v-for 迭代整数
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!--axios  网络请求-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <li v-for="n in 10">{{n}}</li>
      </ul>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
      });
    </script>
  </body>
</html>

输出:

在这里插入图片描述

2.3 事件处理: v-on

  • v-on
<!DOCTYPE html>
<html lang="en">
  <head>
   
    <title>Document</title>
    <!-- 开发版本 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
  
    <div id="some">
      <button v-on:click="greet">Greet</button>
    </div>
 
 
<script>
  var app=new Vue({
    el:'#some',
    data:{
    name:'Vue.js'
    },
    methods: {
      greet:function(event){
        alert(`Hello ${this.name}`);
        if(event){
          alert(event.target.tagName);
        }

      }
    },


  })
  
</script>
</html>

  • v-on: 可简写为@
  • click换成其他事件同样成立,比如鼠标进入事件mouseenter
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <button @mouseenter="number--">-</button>
      <span>{{ number }}</span>
      <button @mouseenter="number++">+</button>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          number: 1,
        },
      });
    </script>
  </body>
</html>

2.4 表单输入绑定 :v-model(双向绑定)

入门 - vue中v-model的实现原理和完整用法详解

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  
  </head>
  <body>
    <div id="app">
      <input type="text" name="name" id="myName" v-model="message" />
      <p>内容为:{{message}}</p>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          message: "Hello",
        },
      });
    </script>
  </body>
</html>

  • v-model结合radio类型使用:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <label for="male">
        <input type="radio" id="male" name="sex" value="" v-model="sex" /></label>
      <label for="female">
        <input type="radio" id="female" name="sex" value="" v-model="sex" /></label>

      <div>你选择的性别为:{{ sex}}</div>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          sex: "男",
        },
      });
    </script>
  </body>
</html>

  • v-model结合复选框类型使用:

单选:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <label for="agree">
        <input
          type="checkbox"
          id="agree"
          value=""
          v-model="isAgree"
        />是否阅读并同意用户协议
      </label>

      <div>你的选择为:{{ isAgree}}</div>
      <button :disabled="!isAgree">下一步</button>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          isAgree: true,
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

在这里插入图片描述

多选:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <input type="checkbox" name="hobby" value="苹果" v-model="choose" />苹果
      <input type="checkbox" name="hobby" value="香蕉" v-model="choose" />香蕉
      <input
        type="checkbox"
        name="hobby"
        value="车厘子"
        v-model="choose"
      />车厘子
      <div>你的选择为:{{ choose}}</div>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          choose: [],
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

  • 值的绑定:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <label :for="item" v-for="(item, index) in hhobbies" :key="index">
        <input
          type="checkbox"
          name="hobby"
          :value="item"
          v-model="hobby"
        />{{item}}
      </label>

      <div>你的选择为:{{ hobby}}</div>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          hhobbies: ["肖战", "王一博", "谢怜"],
          hobby: [],
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

  • v-model结合select实现单选或者多选:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <!-- 单选 -->
      <select name="pinPai" v-model="pinPai">
        <option value="纪梵希">纪梵希</option>
        <option value="YSL">YSL</option>
        <option value="雅诗兰黛">雅诗兰黛</option>
      </select>
      <div>你的选择为:{{ pinPai }}</div>
      <!-- 多选 按住ctrl键实现多选-->
      <select name="pinPais" v-model="pinPais" multiple>
        <option value="纪梵希">纪梵希</option>
        <option value="YSL">YSL</option>
        <option value="雅诗兰黛">雅诗兰黛</option>
      </select>

      <div>你的选择为:{{ pinPais}}</div>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          pinPai: "纪梵希",
          pinPais: [],
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

2.5 计算属性:computed

  • computed
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!--axios  网络请求-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  </head>
  <body>
    <div id="app">
      <p>原始字符串为:{{message}}</p>
      <p>翻转后的字符串为:{{reversedMessage}}</p>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          message: "Hello",
        },
        computed: {
          //计算属性的getter
          reversedMessage: function () {
            return this.message.split("").reverse().join();
          },
        },
      });
    </script>
  </body>
</html>

输出:
在这里插入图片描述

2.6 监听属性:watch

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p>计数器:{{count}}</p>
      <button @click="count++">点击计数</button>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          count: 1,
        },
        watch: {
          count: function (newvalue, oldValue) {
            this.count = newvalue;
            // alert("原值:" + oldValue + "新值:" + newvalue);
          },
        },
      });
    </script>
  </body>
</html>

2.7 样式绑定: v-bind

Vue - v-bind

v-bind 指令用于动态绑定 HTML 属性和组件属性。通过 v-bind 指令,可以将 Vue 实例中的数据动态地绑定到 HTML 元素的属性上,从而实现数据的动态更新响应式渲染

2.7.1 使用v-bind指令:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <img v-bind:src="img" alt="Image" />
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          img: "search.png",
        },
      });
    </script>
  </body>
</html>

输出:

在这里插入图片描述

  • v-bind语法糖(简写)
 <img :src="img" alt="Image" />

2.7.2 v-bind动态绑定class

  • 基本使用:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <!-- <h1 :class="className">标题</h1> -->
      <h1 :class="className">标题</h1>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          className: "active",
        },
      });
    </script>
  </body>
</html>

  • class动态绑定 ( 对象语法 )

对象语法有下面这些用法

(1)直接通过{ }绑定一个类 : <h2 :class="{'active': isActive}">Hello World</h2>
(2)可以通过判断, 传入多个值 : <h2 :class="{'active': isActive, 'line': isLine}">Hello World</h2>
(3)和普通的类同时存在 : <h2 class="title" :class="{'active': isActive}">Hello World</h2>

对象里面的参数是以键值对形式存在的.
键为类名, 值为布尔值.
当值为true的时候, 这个类就会被添加到标签上面
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <!-- <h1 :class="className">标题</h1> -->
      <h1 :class="{active: isActive}">动态绑定</h1>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          isActive: false,
        },
      });
    </script>
  </body>
</html>

isActive为true时候h1的颜色为red

  • 示例:点击实现字体变色:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1 :class="{active:isActive}">标题</h1>
      <button v-on:click="btnClick">点击变换颜色</button>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          isActive: true,
        },
        methods: {
          btnClick: function () {
            this.isActive = !this.isActive;
          },
        },
      });
    </script>
  </body>
</html>

  • class的对象提取为一个方法并调用:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1 :class="getClasses()">标题</h1>
      <button v-on:click="btnClick">点击变换颜色</button>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          isActive: true,
        },
        methods: {
          getClasses: function () {
            return { active: this.isActive };
          },
          btnClick: function () {
            this.isActive = !this.isActive;
          },
        },
      });
    </script>
  </body>
</html>

  • class动态绑定 ( 数组语法 ):
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <ul>
        <li
          v-for="(item, index) in items"
          :class="{active:index==currentIndex}"
          v-on:click="setIndex(index)"
        >
          {{item}}
        </li>
      </ul>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          currentIndex: 0,
          items: ["大话西游1", "大话西游2", "大话西游3"],
        },
        methods: {
          setIndex: function (index) {
            this.currentIndex = index;
          },
        },
      });
    </script>
  </body>
</html>

  • v-bind动态绑定style
    语法:
<h2 :style="{key(属性名): value(属性值)}"></h2>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 开发版本 -->
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style>
      .active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1 :style="{'font-size': '50px'}">标题1</h1>
      <h1 :style="{'font-size': size}">标题2</h1>
    </div>

    <script>
      var app = new Vue({
        el: "#app",
        data: {
          size: "100px",
        },
      });
    </script>
  </body>
</html>

输出:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值