20231012-黑马Vue学习笔记-第一天

Vue的概念

Vue 是一个用于 构建用户界面 的 渐进式 框架

Vue2官网

https://v2.cn.vuejs.org/

创建一个Vue实例

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <!--引包 开发环境版本,包含了有帮助的命令行警告 -->
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    <!-- 编写用于渲染的代码逻辑 -->
    <h1>
      <!-- 插值表达式 语法:{{表达式}} -->
      <!-- 注意事项1:使用数据要存在 -->
      <!-- 注意事项2:支持的是表达式,而不是语句 if for -->
      <!-- 注意事项3:不能在标签属性中使用 -->
      {{msg}}
    </h1>
    <p>{{friend.name+' '+(friend.age>=18?'成年':'未成年')}}</p>
  </div>

  <script>
    // Vue构造函数
    const app = new Vue({
      // 通过el配置选择器,指定Vue管理哪个盒子
      el: "#app",
      // 通过data提供数据
      data: {
        msg: "Hello world",
        friend: {
          name: 'Jony',
          age: 18
        }
      }
    });

    // Vue响应式特性
    // data中的数据是会被添加到实例上
    // 1.访问数据 实例.属性名 app.msg
    console.log(app.friend.name);
    // 2.修改数据 实例.属性名 = 新值
    app.msg = "你好,世界!";
  </script>
</body>

</html>

安装Vue开发者工具

https://chrome.zzzmh.cn/index#/index

Vue指令

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <!-- 开发环境版本,包含了有帮助的命令行警告 -->
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>

  <div id="app">
    <!-- v-html 设置元素的innerHTML 解析字符串标签 -->
    <div v-html="msg"></div>

    <!-- 切换 display:none 控制元素显示隐藏  -->
    <h2 v-show="falg">Hello world!</h2>

    <!-- 通过创建或移除元素节点来控制元素的显示和隐藏 -->
    <h2 v-if="falg">你好,世界!</h2>

    <!-- v-else v-else-if 辅助 v-if 进行判断渲染 -->
    <h2 v-if="gender===1">性别:♂男</h2>
    <h2 v-else>性别:♀女</h2>

    <h2 v-if="score>=90">优秀</h2>
    <h2 v-else-if="score>=80">良好</h2>
    <h2 v-else-if="score>=60">及格</h2>
    <h2 v-else>不及格</h2>

    <!-- v-on 注册事件 = 添加监听 + 提供处理逻辑 v-on: 可简写为 @ -->
    <!-- v-on:事件名 = "内联语句" -->
    <div>
      <button v-on:click="count--">-</button>
      <span>{{count}}</span>
      <button @click="count++">+</button>
    </div>

    <!-- v-on:事件名="methods中的函数名" -->
    <div>
      <button @click="ShowOrHide">切换显示隐藏</button>
      <span v-show="isShow">天生我材必有用,千金散尽还复来</span>
    </div>

    <!-- v-on调用传参 -->
    
    <div style="border: 2px solid red; width: 200px; margin-top: 20px;">
      <h3>小黑自动售货机</h3>
      <button @click="Buy(5)">可乐5元</button>
      <button @click="Buy(10)">咖啡10元</button>
      <p>银行卡余额:{{money}}元</p>
    </div>

    <!-- v-bind 动态设置html的标签属性 语法 v-bind:属性名="表达式" 可简写为 :属性名="表达式" -->
    <div>
      <img v-bind:src="imgUrl" alt="" style="width: 180px; height: 120px;">
    </div>

    <!-- 基于数据循环 多次渲染整个数据 语法 v-for="(item,index) in 数组|对象|数字" item 每一项 index 下标 -->
    <div>
      <h3>小黑水果店</h3>
      <ul>
        <li v-for="(item,index) in list">{{item}}-{{index}}</li>
        <li v-for="item in list">{{item}}</li>
      </ul>
    </div>

    <!-- key的作用 给元素添加唯一标识 便于Vue进行列表项的正确排序复用 -->
    <!-- <li v-for="(item,index) in xxx" :key="唯一值"></li> -->

    <!-- v-model 给表单元素使用 双向数据绑定 可以快速 获取 或 设置 表单元素 -->
    <div style="width: 250px; height: 140px; border: 1px solid black; text-align: center;">
      账户:<input type="text" v-model="username">
      <br><br>
      密码:<input type="password" v-model="password">
      <br><br>
      <button @click="login">登录</button>
      <button @click="reset">重置</button>
    </div>
  </div>



  <script>
    const app = new Vue({
      el: '#app',
      data: {
        msg: "<h1>Nice day!</h1>",
        falg: false,
        gender: 2,
        score: 59,
        count: 100,
        isShow: false,
        money: 100,
        imgUrl: "./images/1.jpg",
        list: ['苹果', '香蕉', '梨'],
        username: '',
        password: ''
      },
      methods: {
        ShowOrHide() {
          this.isShow = !this.isShow;
        },
        Buy(price) {
          this.money -= price;
        },
        login() {
          if (this.username === 'admin' && this.password === 'admin') {
            alert('登陆成功!');
          } else {
            alert('登录失败!');
          }
        },
        reset() {
          this.username = '';
          this.password = '';
        }
      }
    });
  </script>

</body>

</html>

案例-波仔的学习之旅

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #box {
      width: 480px;
      height: 640px;
      background-color: aquamarine;
      margin: 100px auto;
      text-align: center;
    }

    #box img {
      width: 240px;
      height: 240px;
      margin: 100px auto;
      border: 1px solid black;
    }

    .btns {
      height: 80px;
      background-color: skyblue;
    }

    .btns button {
      width: 100px;
      height: 40px;
      border-radius: 20px;
      margin-top: 20px;
    }

    .lastBtn {
      float: left;
      margin-left: 100px;
    }

    .nextBtn {
      float: right;
      margin-right: 100px;
    }
  </style>
</head>

<body>
  <div id="box">
    <h2>波仔的学习之旅</h2>
    <img :src="imgsUrl[index]" alt="">

    <div class="btns">
      <button class="lastBtn" v-show="index > 0" @click="index--">上一页</button>
      <button class="nextBtn" v-show="index < imgsUrl.length-1" @click="index++">下一页</button>
    </div>
  </div>

  <script>
    const app = new Vue({
      el: "#box",
      data: {
        imgsUrl: ['./images/波仔/10-01.png', './images/波仔/10-02.png', './images/波仔/11-00.gif', './images/波仔/11-01.gif', './images/波仔/11-02.gif', './images/波仔/11-03.gif', './images/波仔/11-04.png', './images/波仔/11-05.png'],
        index: 0,
      }
    });
  </script>

</body>

</html>

常见的事件类型

  • 鼠标事件:
    • click:鼠标点击事件
    • dblclick:鼠标双击事件
    • mousedown:鼠标按下事件
    • mouseup:鼠标松开事件
    • mousemove:鼠标移动事件
    • mouseover:鼠标移入事件
    • mouseout:鼠标移出事件

  • 键盘事件:
    • keydown:按下键盘按键事件
    • keyup:松开键盘按键事件
    • keypress:按下并松开键盘按键事件

  • 表单事件:
    • input:输入事件,当输入框的值发生变化时触发
    • change:值改变事件,当表单元素的值发生变化时触发
    • submit:表单提交事件
    • focus:获得焦点事件
    • blur:失去焦点事件

  • 触摸事件:
    • touchstart:触摸开始事件
    • touchmove:触摸移动事件
    • touchend:触摸结束事件

  • 滚动事件:
    • scroll:滚动事件,当滚动条滚动时触发

案例-小黑的书架

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
  <div id="bookshelf">
    <h1>小黑的书架</h1>
    <ul>
      <li v-for="item in booksList" :key="item.id">{{item.name}} {{item.author}}
        <button @click="del(item.id)">删除</button>
      </li>

    </ul>
  </div>

  <script>
    const app = new Vue({
      el: '#bookshelf',
      data: {
        booksList: [
          { id: 1, name: '《红楼梦》', author: '曹雪芹' },
          { id: 2, name: '《西游记》', author: '吴承恩' },
          { id: 3, name: '《水浒传》', author: '施耐庵' },
          { id: 4, name: '《三国演义》', author: '罗贯中' },
        ],
      },
      methods: {
        del(id) {
          // filter 根据条件 保留满足条件的对应项 得到一个新数组
          this.booksList = this.booksList.filter(item => item.id != id);
        }
      }
    });
  </script>
</body>

</html>

案例-小黑记事本

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #notepad h1 {
      text-align: center;
      padding: 10px;
    }

    .addTask {
      text-align: center;
      padding: 20px;
    }

    .addTask input {
      height: 40px;
      width: 360px;
      font-size: 20px;
    }

    .addTask button {
      height: 40px;
      width: 80px;
      border-radius: 20px;
      margin-left: 10px;
    }

    .items {
      width: 640px;
      background-color: aqua;
      margin: 0 auto;
    }

    .items li {
      list-style: none;
      background-color: cadetblue;
      height: 48px;
      font-size: 24px;
      line-height: 48px;
      text-align: center;
      margin-bottom: 10px;
    }

    .items li button {
      background-color: transparent;
      border: 1px solid transparent;
      font-size: 20px;
      color: brown;
      float: right;
      margin-top: 15px;
      margin-right: 20px;
    }

    .bottomNav {
      height: 40px;
      background-color: deepskyblue;
    }

    .bottomNav span {
      line-height: 40px;
      margin-left: 40px;
    }

    .bottomNav button {
      float: right;
      width: 80px;
      height: 32px;
      border-radius: 16px;
      background-color: darkcyan;
      margin-right: 40px;
      margin-top: 5px;
    }
  </style>
</head>

<body>
  <div id="notepad">
    <h1>小黑记事本</h1>
    <div class="addTask">
      <input type="text" v-model="addTask">
      <button @click="Add">添加任务</button>
    </div>
    <div class="items">
      <ul>
        <li v-for="(item,index) in tasksList" :key="item.id">{{++index}}、{{item.taskName}}
          <button @click=Remove(item.id)>X</button>
        </li>
      </ul>
      <div class="bottomNav" v-show="tasksList.length>0">
        <span>合计:{{tasksList.length}}</span>
        <button @click="Reset">清空任务</button>
      </div>
    </div>
  </div>

  <script>
    const app = new Vue({
      el: "#notepad",
      data: {
        tasksList: [
          { id: 1, taskName: '跑步一公里' },
          { id: 2, taskName: '跳绳200个' }
        ],
        addTask: '',
      },
      methods: {
        // unshift 在数组最前面添加数据
        Add() {
          if (this.addTask.trim() === '') {
            alert("请输入任务名称!!!");
            return;
          }
          let newTask = { id: this.tasksList.length + 1, taskName: this.addTask };
          this.tasksList.push(newTask);
          this.addTask = '';
        },
        Reset() {
          this.tasksList = [];
        },
        Remove(id) {
          this.tasksList = this.tasksList.filter(item => item.id != id);
        }
      }
    });
  </script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值