首页 > 其他分享 >Chapter 03 Vue指令(下)

Chapter 03 Vue指令(下)

时间:2024-08-25 21:25:33浏览次数:7  
标签:Chapter 03 Vue app list font todo id

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅!

文章目录


前言

在 Vue.js 中,指令是带有 v- 前缀的特殊属性,不同属性对应不同的功能。通过学习不同的指令,我们能够灵活应对多种业务场景的需求。本章详细讲解了一些基本的 Vue 指令,包括 v-on、v-for、v-bind 以及 v-model 指令。

一、v-on指令

①作用:给元素绑定事件监听器

②语法:

  • v-on:事件名 = "内联语句"
  • v-on:事件名 = "methods中的函数名"
特性内联语句methods中的函数名
定义方式直接在模板中编写表达式或语句methods中定义函数
适用场景简单操作复杂逻辑或多个操作
可读性可读性较差,逻辑复杂时难以理解可读性好,更易于维护
维护性难以维护,逻辑修改需在模板中直接更改易于维护,逻辑集中处理
复用性不能复用,通常只适用于当前事件可复用,一个方法可被多次调用

③简写:v-on:事件名 = @事件名

④v-on调用传参:
通常情况下,可以直接将方法名绑定到事件上,但在某些情况下需要在调用方法时传入参数。
语法: v-on:事件名 = "methodName(parameter)"
在这里插入图片描述

⑤修饰符:

修饰符描述
.stop调用 event.stopPropagation()
.prevent调用 event.preventDefault()
.capture在捕获模式添加事件监听器。
.self只有事件从元素本身发出才触发处理函数。
. {keyAlias}只在某些按键下触发处理函数。
.once最多触发一次处理函数。
.left只在鼠标左键事件触发处理函数。
.right只在鼠标右键事件触发处理函数。
.middle只在鼠标中键事件触发处理函数。
.passive通过 { passive: true } 附加一个 DOM 事件。

⑥常用的监听事件:

事件描述
click点击事件
dblclick双击事件
mouseenter鼠标进入元素时触发
mouseleave鼠标离开元素时触发
keydown按键按下事件
keyup按键松开事件
input输入框内容变化时触发
change表单元素内容变化后触发
submit提交表单时触发
focus输入框获得焦点时触发
blur输入框失去焦点时触发
scroll元素滚动时触发
resize窗口或元素大小改变时触发
transitionendCSS 过渡结束时触发
animationendCSS 动画结束时触发
contextmenu右键点击事件

【示例一】
语法1:v-on:事件名 = "内联语句"
内联语句(Inline Statement)是指在某个上下文环境中直接书写的、可以立即执行的代码表达式。这种写法通常不需要额外的命名或定义,而是在特定条件下直接被调用。在Vue.js等框架中,可以快速实现事件处理逻辑,适合于简单的场景和快速开发。

<body>
  <div id="app">
    <!-- 减号按钮,点击时通过v-on:click指令绑定点击事件,触发count--,count的值减1 -->
    <button v-on:click="count--">-</button>
    <span>{{count}}</span>
    <!-- 加号按钮,点击时通过v-on:click指令绑定点击事件,触发count++,count的值加1 -->
    <button v-on:click="count++">+</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        /* count初始值为100 */
        count:100
      }
    })
  </script>
</body>

运行结果:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
v-on:事件名可以简写成@事件名

<div id="app">
    <button @click="count--">-</button>
    <span>{{count}}</span>
    <button @click="count++">+</button>
  </div>

【示例二】
语法2:v-on:事件名 = "methods中的函数名"

<body>
  <div id="app">
    <button @click="fn">切换显示隐藏</button>
    <h1 v-show="isShow">Hello World</h1>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        isShow:true
      },
      methods:{/* 定义了Vue实例的方法fn,功能是切换isShow的值(如果为true则变为false,反之亦然) */
        fn(){
          /* this关键字指向当前的Vue实例app,用于访问组件的属性、数据和方法 */
          this.isShow=!this.isShow
        }
      }
    })
  </script>
</body>

运行结果:
在这里插入图片描述
【示例三】
v-on参数调用: v-on:事件名 = "methodName(parameter)"

<!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>v-on调用参数</title>
  <style>
    .box {
      border: 3px solid #000000;
      border-radius: 10px;
      padding: 20px;
      margin: 20px;
      width: 150px;
    }
    h3 {
      margin: 10px 0 20px 0;
    }
    p {
      margin: 20px;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="box">
      <h3>饮料自动售货机</h3>
      <!-- 调用buy方法并传入相应的价格参数 -->
      <button @click="buy(5)">可乐5元</button>
      <button @click="buy(10)">咖啡10元</button>
    </div>
    <p>用户余额:{{money}}</p>
  </div>

  <script src="./vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        /* 初始金额为100 */
        money:100
      },
      methods:{
        buy(price){
          /* this关键字确保我们访问的是Vue实例中的money属性,而不是全局的变量 */
          this.money-=price
        }
      }
    })
  </script>
</body>
</html>

运行结果:
在这里插入图片描述
点击“可乐5元”按钮,余额减5,剩余95元:
在这里插入图片描述
再点击“咖啡10元”按钮,余额减10,剩余85元:
在这里插入图片描述

二、v-for指令

①作用:基于原始数据多次渲染元素或模板块。

②遍历数组的语法:v-for = "(item, index) in 数组"

  • 参数item:表示当前遍历到的元素,它会依次被赋值为数组中的每个元素
  • 参数index:表示当前元素在数组中的索引(位置),从 0 开始递增

【示例】

<body>

  <div id="app">
    <h3>水果店</h3>
    <ul>
      <!-- v-for 指令用于循环遍历 list 的数据属性 -->
      <li v-for="(item,index) in list">{{item}}</li>
    </ul>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        list: ['西瓜', '苹果', '鸭梨']
      }
    })
  </script>
</body>

运行结果:
在这里插入图片描述
③key属性:
在使用v-for渲染列表时,Vue会根据每个项目的key值来追踪节点的身份。这个机制使得Vue在更新虚拟DOM时能够准确地从旧的节点中找到对应的节点,从而进行最小化的更新,进一步提高性能,并确保组件的状态在更新过程中得到保持。

  • key 的值只能是字符串数字类型
  • key 的值必须具有唯一性
  • 推荐使用 id 作为 key(唯一),不推荐使用 index 作为 key(会变化,不对应)
<body>

  <div id="app">
    <h3>我的书架</h3>
    <ul>
      <!-- 通过将 item.id 作为 key,Vue 可以识别每本书的唯一性 -->
      <li v-for="(item,index) in booksList" :key="item.id">
        <span>{{item.name}}</span>
        <span>{{item.author}}</span>
        <button @click="del(item.id)">删除</button>
      </li>
    </ul>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        booksList: [
          { id: 1, name: '《红楼梦》', author: '曹雪芹' },
          { id: 2, name: '《西游记》', author: '吴承恩' },
          { id: 3, name: '《水浒传》', author: '施耐庵' },
          { id: 4, name: '《三国演义》', author: '罗贯中' }
        ]
      },
      methods: {
        /* del(id):根据传入的 id 删除对应的书籍

           filter:一个数组方法,用于创建一个新数组,该数组包含所有通过指定测试的元素。
           该方法不会改变原始数组,而是返回一个新的数组。
           
           item => item.id !== id:一个 箭头函数 (arrow function),被用作 filter 方法中的回调函数;
           其中item.id !== id是一个条件表达式,它检查当前书籍的 id 是否不等于传入的 id;
           如果两者不相等,则返回 true,条件满足,当前的 item 被保留在新数组中;
           如果两者相等,则返回 false,条件不满足,这样当前的 item 不会出现在新数组中。
           */
        del(id){
          this.booksList = this.booksList.filter(item => item.id !== id);  
        }
      },
    })
  </script>
</body>

运行结果:
在这里插入图片描述

删除《红楼梦》后:
在这里插入图片描述
接下来我们通过一个直观的例子进一步理解:
v-for不加key值,默认会原地修改元素(就地复用)。删除一个列表项后,后面的项会向前移动占据原位置,但 Vue 还会保留它们的旧状态。

<ul>
  <li v-for="(item,index) in booksList">
    <span>{{item.name}}</span>
    <span>{{item.author}}</span>
    <button @click="del(item.id)">删除</button>
  </li>
</ul>

在这里插入图片描述
删除《红楼梦》后:
在这里插入图片描述
v-forkey值,Vue 会使用key追踪每个列表项的身份。当删除一个项时,Vue 可以准确地识别出该项并仅删除对应的 DOM 元素,而不是重新渲染整个列表。

<ul>
  <!-- 通过将 item.id 作为 key,Vue 可以识别每本书的唯一性 -->
  <li v-for="(item,index) in booksList" :key="item.id">
    <span>{{item.name}}</span>
    <span>{{item.author}}</span>
    <button @click="del(item.id)">删除</button>
  </li>
</ul>

在这里插入图片描述
删除《红楼梦》后:
在这里插入图片描述

三、v-bind指令

①作用:动态地设置html的标签属性,允许根据 Vue 实例中的数据动态地更新和渲染元素的属性。

②语法: v-bind:属性名="表达式"

③简写::属性名="表达式

【示例】

<body>
  <div id="app">
    <!-- 使用 v-bind 指令将 imgUrl 变量的值绑定到 <img> 元素的 src 属性上,使其动态显示 
         当 imgUrl 的值改变时,图片的源地址也会自动更新-->
    <img v-bind:src="imgUrl" alt="">
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        imgUrl:'./imgs/Vue图片.png'
      }
    })

  </script>
</body>

运行结果:
在这里插入图片描述
v-bind:属性名="表达式"可以简写为:属性名="表达式

<img :src="imgUrl" alt="">

【案例】
根据所学内容实现图片切换。
在这里插入图片描述
核心思路分析:
① 数组存储图片路径 → [ 图片1, 图片2, 图片3, … ]
② 准备下标 index,数组[下标] → v-bind 设置 src 展示图片 → 修改下标切换图片

<body>
  <div id="app">
    <!-- 
    v-show="index>0": 控制按钮的显示或隐藏。当index>0时(表示不是在第一张图片),按钮会显示;否则,按钮会隐藏
    @click="index--": 当用户点击此按钮时,index 的值会-1,切换到前一张图片
    -->
    <button v-show="index>0" @click="index--">上一页</button>
    <div>
      <!-- list[index] 获取当前显示的图片路径 -->
      <img v-bind:src="list[index]" alt="">
    </div>
    <!-- 
    v-show="index<list.length-1": 控制按钮的显示或隐藏。当index<list.length-1时(表示不是在最后一张图片),按钮会显示;否则,按钮会隐藏
    @click="index++": 当用户点击此按钮时,index 的值会+ 1,切换到下一张图片。 
    -->
    <button v-show="index<list.length-1" @click="index++">下一页</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        index:0,
        list: [
          './imgs/photo01.jpg',
          './imgs/photo02.jpg',
          './imgs/photo03.jpg',
          './imgs/photo04.jpg',
          './imgs/photo05.jpg',
          './imgs/photo06.jpg',
          './imgs/photo07.jpg',
          './imgs/photo08.jpg'
        ]
      }
    })
  </script>
</body>

运行结果:
第一张图片:
在这里插入图片描述

点击下一页后切换到第二张图片:
在这里插入图片描述

最后一张图片:
在这里插入图片描述

四、v-model指令

①作用:用于表单元素,实现数据与视图的双向绑定,可快速获取或设置表单元素内容

  • 数据变化 → 视图自动更新
  • 视图变化 → 数据自动更新
    在这里插入图片描述

②语法: v-model="变量"

③修饰符:

修饰符说明
.lazy将输入的更新延迟到 change 事件被触发。
.number将输入的值自动转为数字类型。
.trim自动去除输入首尾的空白字符。
.prop绑定到组件的 prop,适用于自定义组件。
.once只绑定一次输入值,随后的更改将不会触发更新。

【示例】

<body>

  <div id="app">
    账户:<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>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        username:'',
        password:''
      },
      methods: {
        login(){
          console.log(this.username,this.password)
        },
        reset(){
          this.username=''
          this.password=''
        }
      },
    })
  </script>
</body>

运行结果:
输入用户名密码:
在这里插入图片描述
点击登录:
在这里插入图片描述
点击重置:
在这里插入图片描述
【综合案例】
根据所学内容实现下方的记事本,要求实现任务的添加、删除以及清空。
在这里插入图片描述
CSS代码:

html,
body {
  margin: 0;
  padding: 0;
}
body {
  background: #fff;
}
button {
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font-size: 100%;
  vertical-align: baseline;
  font-family: inherit;
  font-weight: inherit;
  color: inherit;
  -webkit-appearance: none;
  appearance: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
  line-height: 1.4em;
  background: #f5f5f5;
  color: #4d4d4d;
  min-width: 230px;
  max-width: 550px;
  margin: 0 auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-weight: 300;
}

:focus {
  outline: 0;
}

.hidden {
  display: none;
}

#app {
  background: #fff;
  margin: 180px 0 40px 0;
  padding: 15px;
  position: relative;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
#app .header input {
  border: 2px solid rgba(175, 47, 47, 0.8);
  border-radius: 10px;
}
#app .add {
  position: absolute;
  right: 15px;
  top: 15px;
  height: 68px;
  width: 140px;
  text-align: center;
  background-color: rgba(175, 47, 47, 0.8);
  color: #fff;
  cursor: pointer;
  font-size: 18px;
  border-radius: 0 10px 10px 0;
}

#app input::-webkit-input-placeholder {
  font-style: italic;
  font-weight: 300;
  color: #e6e6e6;
}

#app input::-moz-placeholder {
  font-style: italic;
  font-weight: 300;
  color: #e6e6e6;
}

#app input::input-placeholder {
  font-style: italic;
  font-weight: 300;
  color: gray;
}

#app h1 {
  position: absolute;
  top: -120px;
  width: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 60px;
  font-weight: 100;
  text-align: center;
  color: rgba(175, 47, 47, 0.8);
  -webkit-text-rendering: optimizeLegibility;
  -moz-text-rendering: optimizeLegibility;
  text-rendering: optimizeLegibility;
}

.new-todo,
.edit {
  position: relative;
  margin: 0;
  width: 100%;
  font-size: 24px;
  font-family: inherit;
  font-weight: inherit;
  line-height: 1.4em;
  border: 0;
  color: inherit;
  padding: 6px;
  box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.new-todo {
  padding: 16px;
  border: none;
  background: rgba(0, 0, 0, 0.003);
  box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);
}

.main {
  position: relative;
  z-index: 2;
}

.todo-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
}

.todo-list li {
  position: relative;
  font-size: 24px;
  height: 60px;
  box-sizing: border-box;
  border-bottom: 1px solid #e6e6e6;
}

.todo-list li:last-child {
  border-bottom: none;
}

.todo-list .view .index {
  position: absolute;
  color: gray;
  left: 10px;
  top: 20px;
  font-size: 22px;
}

.todo-list li .toggle {
  text-align: center;
  width: 40px;
  /* auto, since non-WebKit browsers doesn't support input styling */
  height: auto;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: auto 0;
  border: none; /* Mobile Safari */
  -webkit-appearance: none;
  appearance: none;
}

.todo-list li .toggle {
  opacity: 0;
}

.todo-list li .toggle + label {
  /*
		Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433
		IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/
	*/
  background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E');
  background-repeat: no-repeat;
  background-position: center left;
}

.todo-list li .toggle:checked + label {
  background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E');
}

.todo-list li label {
  word-break: break-all;
  padding: 15px 15px 15px 60px;
  display: block;
  line-height: 1.2;
  transition: color 0.4s;
}

.todo-list li.completed label {
  color: #d9d9d9;
  text-decoration: line-through;
}

.todo-list li .destroy {
  display: none;
  position: absolute;
  top: 0;
  right: 10px;
  bottom: 0;
  width: 40px;
  height: 40px;
  margin: auto 0;
  font-size: 30px;
  color: #cc9a9a;
  margin-bottom: 11px;
  transition: color 0.2s ease-out;
}

.todo-list li .destroy:hover {
  color: #af5b5e;
}

.todo-list li .destroy:after {
  content: '×';
}

.todo-list li:hover .destroy {
  display: block;
}

.todo-list li .edit {
  display: none;
}

.todo-list li.editing:last-child {
  margin-bottom: -1px;
}

.footer {
  color: #777;
  padding: 10px 15px;
  height: 20px;
  text-align: center;
  border-top: 1px solid #e6e6e6;
}

.footer:before {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 50px;
  overflow: hidden;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), 0 8px 0 -3px #f6f6f6,
    0 9px 1px -3px rgba(0, 0, 0, 0.2), 0 16px 0 -6px #f6f6f6,
    0 17px 2px -6px rgba(0, 0, 0, 0.2);
}

.todo-count {
  float: left;
  text-align: left;
}

.todo-count strong {
  font-weight: 300;
}

.filters {
  margin: 0;
  padding: 0;
  list-style: none;
  position: absolute;
  right: 0;
  left: 0;
}

.filters li {
  display: inline;
}

.filters li a {
  color: inherit;
  margin: 3px;
  padding: 3px 7px;
  text-decoration: none;
  border: 1px solid transparent;
  border-radius: 3px;
}

.filters li a:hover {
  border-color: rgba(175, 47, 47, 0.1);
}

.filters li a.selected {
  border-color: rgba(175, 47, 47, 0.2);
}

.clear-completed,
html .clear-completed:active {
  float: right;
  position: relative;
  line-height: 20px;
  text-decoration: none;
  cursor: pointer;
}

.clear-completed:hover {
  text-decoration: underline;
}

.info {
  margin: 50px auto 0;
  color: #bfbfbf;
  font-size: 15px;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  text-align: center;
}

.info p {
  line-height: 1;
}

.info a {
  color: inherit;
  text-decoration: none;
  font-weight: 400;
}

.info a:hover {
  text-decoration: underline;
}

/*
	Hack to remove background from Mobile Safari.
	Can't use it globally since it destroys checkboxes in Firefox
*/
@media screen and (-webkit-min-device-pixel-ratio: 0) {
  .toggle-all,
  .todo-list li .toggle {
    background: none;
  }

  .todo-list li .toggle {
    height: 40px;
  }
}

@media (max-width: 430px) {
  .footer {
    height: 50px;
  }

  .filters {
    bottom: 10px;
  }
}

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" />
<link rel="stylesheet" href="./css/index.css" />
<title>记事本</title>
</head>
<body>

<!-- 主体区域 -->
<section id="app">
  <!-- 输入框 -->
  <header class="header">
    <h1>记事本</h1>
    <!-- 使用 v-model 双向绑定 todoName,用户可以在输入框中输入任务名称 -->
    <input v-model="todoName"   placeholder="请输入任务" class="new-todo" />
    <button class="add" @click="add()">添加任务</button>
  </header>
  <!-- 列表区域 -->
  <section class="main">
    <ul class="todo-list">
      <!-- v-for 循环 list 数组,生成每个任务的内容 -->
      <li class="todo" v-for="(item,index) in list" :key="item.id">
        <div class="view">
          <span class="index">{{index+1}}.</span> <label>{{item.name}}</label>
          <button class="destroy" @click="del(item.id)"></button>
        </div>
      </li>
    </ul>
  </section>
  <!-- 统计和清空 -->
   <!-- 仅在 list 不为空时显示统计和清空按钮 -->
  <footer class="footer" v-show="list.length>0">
    <!-- 统计 -->
    <span class="todo-count">合 计:<strong> {{list.length}} </strong></span>
    <!-- 清空 -->
    <button class="clear-completed" @click="clear">
      清空任务
    </button>
  </footer>
</section>

<!-- 底部 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>

  const app = new Vue({
    el: '#app',
    data: {
      todoName:'',
     list:[
      {id:1,name:'跑步一公里'},
      {id:2,name:'学习两小时'},
      {id:3,name:'跳绳200下'}
     ]
    },methods:{
      del(id){
        this.list=this.list.filter(item=>item.id!==id)
      },
      add(){
        /* 检查输入框是否为空 (trim() 去除前后空格) */
        if(this.todoName.trim()==''){
          alert('请输入任务事项')
          return
        }
        this.list.unshift({
          id:+new Date(),
          name:this.todoName
        })
        this.todoName=''
      },
      clear(){
        /* 将 list 重置为空数组,清空所有任务 */
        this.list=[]
      }
    }
  })

</script>
</body>
</html>

运行结果:
在这里插入图片描述
添加“游泳一小时”任务:
在这里插入图片描述
删除“跑步一公里”任务:
在这里插入图片描述
清空任务:
在这里插入图片描述
添加空白任务:
在这里插入图片描述

标签:Chapter,03,Vue,app,list,font,todo,id
From: https://blog.csdn.net/2302_80253507/article/details/141441372

相关文章

  • 题解:CF70D Professor's task
    题意实现以下两种操作:往点集\(S\)中添加一个点\((x,y)\)。询问点\((x,y)\)是否在点集\(S\)的凸包中。分析动态凸包板子。建议先完成P2521[HAOI2011]防线修建。上题维护的是上半个凸包,本题维护上下两个。将凸包中的点按\(x\)排序,通过\((x,y)\)前驱......
  • 基于springboot+vue.js的牙科就诊管理系统附带文章源码部署视频讲解等
    文章目录前言详细视频演示具体实现截图核心技术介绍后端框架SpringBoot前端框架Vue持久层框架MyBaits为什么选择我代码参考数据库参考测试用例参考源码获取前言......
  • 基于ssm+vue.js的附学费管理系统带文章源码部署视频讲解等
    文章目录前言详细视频演示具体实现截图核心技术介绍后端框架SSM前端框架Vue持久层框架MyBaits为什么选择我代码参考数据库参考测试用例参考源码获取前言......
  • [Vue] useVModel
    Onewaydatabinding,theparentcomponentpasingdatathrough v-modeltochildcomponent,ifchildmodifythedata, v-modelwilltakecareemitthechangedbacktoparentcomponent.Thisparttenworksfine.Buttheremightbesomeproblem,forexample,wha......
  • 基于Springboot+Vue的铁路订票管理系统(含源码数据库)
    1.开发环境开发系统:Windows10/11架构模式:MVC/前后端分离JDK版本:JavaJDK1.8开发工具:IDEA数据库版本:mysql5.7或8.0数据库可视化工具:navicat服务器:SpringBoot自带apachetomcat主要技术:Java,Springboot,mybatis,mysql,vue2.视频演示地址3.功能这个系......
  • Vue3项目开发——新闻发布管理系统(三)
    文章目录七、登录&注册页面设计开发1、安装、导入`ElementPlus`组件库1.1安装1.2引用1.3设置中文1.4使用七、登录&注册页面设计开发网页页面设计肯定离不开表单、输入框、按钮等等控件的布局、样式等等的设计。在新闻发布管理系统中,使用ElementPlus......
  • idea控制vue项目启动(前后端分离中的前端)(自用
    EditControl…… npm 选中vue的package.json(后端注意设置端口为8080之外的) scripts选serve 启动时不要使用debug模式 ......
  • POLIR-Society-Organization-真实社政: 人性{黑、白、灰}: + 管理Strategy的(整体/组织
    手机实名制+虚拟卡号手机实名制防止电诈减少犯罪发生;虚拟卡号确实有正面意义与负面意义正面意义:"虚拟号"的政策本身是好的没问题的;例如,社会性的研究;即使“不法分子”使用“虚拟号”诈骗犯罪,群众的“警惕性”更高更易察觉;因为:“虚拟号段”已经“预先分类”过,筛选......
  • [vue3] vue3 setup函数
    从语法上看,CompositionAPI提供了一个setup启动函数作为逻辑组织的入口,提供了响应式API,提供了生命周期函数以及依赖注入的接口,通过调用函数来声明一个组件。OptionsAPI选项式API在props、data、methods、computed等选项中定义变量;在组件初始化阶段,Vue.js内部处理这......
  • 基于django+vue摄影网站的设计与实现【开题报告+程序+论文】计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展和普及,数字化摄影已成为大众生活的一部分,摄影作品的传播与分享方式也发生了深刻变革。传统的摄影展示与交易方式......