美文网首页
computed 计算属性

computed 计算属性

作者: lucky_yao | 来源:发表于2020-10-14 07:26 被阅读0次
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>computed</title>
    <style>
      .active {
        color: rgb(142, 207, 44);
      }
    </style>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="username" placeholder="姓名" />
      <input type="text" v-model="genboy" placeholder="性别" />
      <button @click="abb">添加</button>
      <button @click="del()">删除</button>
      <!-- <button @click="showUser('')">全部</button>
      <button @click="showUser('男')">男生</button>
      <button @click="showUser('女')">女生</button> -->

      <ul>
        <li v-for="(user,i) in getuser" :key="i">
          <input type="checkbox" v-model="user.checked" />
          编号:{{i+1}} 姓名:{{user.username}} 性别:{{user.gender}}
        </li>
      </ul>
      <button :class="{active:gender==''}" @click="gender = ''">全部</button>
      <button :class="{active:gender=='男'}" @click="gender = '男'">
        男生
      </button>
      <button :class="{active:gender=='女'}" @click="gender = '女'">
        女生
      </button>
      全选:<input type="checkbox" v-model="checkedAll" />
    </div>
  </body>
  <script src="js/vue.js"></script>
  <script>
    let users = [
      { id: 1, username: "ui", gender: "男", checked: false },
      { id: 2, username: "qw", gender: "男", checked: false },
      { id: 3, username: "er", gender: "男", checked: false },
      { id: 4, username: "ty", gender: "女", checked: false },
      { id: 5, username: "io", gender: "男", checked: false },
      { id: 6, username: "po", gender: "女", checked: false },
      { id: 7, username: "sw", gender: "男", checked: false },
    ];
    let app = new Vue({
      el: "#app",
      data: {
        users,
        // getuser: users,
        gender: "",
        username: "",
        genboy: "",
      },
      computed: {
        getuser: {
          get() {
            if (this.gender == "") {
              return this.users;
            } else {
              // user里的gender的值传回值 是返回符合值得所有 值
              return this.users.filter((user) => user.gender == this.gender);
            }
          },
        },
        checkedAll: {
          // checkedAll 他的值是由每个checked的true/false觉得的 默认为false
          get() {
            //   every 映射返回一个新的数组 不会动原数组
            return this.users.every((user) => user.checked);
          },
          set(newVal) {
            //   newVal 是个新值 原本是false 新值是true
            return this.users.forEach((user) => {
              user.checked = newVal;
            });
          },
        },
      },
      methods: {
        //     showUser(gender) {
        //       if (gender == "") {
        //         this.getuser = this.users;
        //       } else {
        //         this.getuser = this.users.filter((user) => user.gender == gender);
        //       }
        //     },
        abb() {
          var reg = /\S/;
          if (reg.test(this.username) && reg.test(this.genboy)) {
            var obj = {
              id: this.users.length + 1,
              username: this.username,
              gender: this.genboy,
              checked: false,
            };
            // console.log(obj.name);
            this.users.push(obj);
          } else {
            alert("不能为空");
          }
          this.username = this.genboy = "";
        },
        del() {
          this.users = this.getuser.filter((user) => user.checked == false);
        },
      },
    });
  </script>
</html>

相关文章

  • 计算属性

    1.计算属性get方法: 计算属性(computed)和Methods区别:计算属性(computed)适合:有缓...

  • Vue复习

    Vue的计算属性 计算属性computed

  • 03.vue3-组合API(下篇)

    组合API-computed函数 定义计算属性: computed函数,是用来定义计算属性的,计算属性不能修改。基...

  • 监听器和计算属性的区别watch,computed

    计算属性computed和监听器watch区别?1.能使用计算属性computed的尽量使用计算属性,但是计算属性...

  • Vue之计算属性computed(一)

    Vue中什么是计算属性computed,计算属性的基础、计算属性computed与方法method实现相同的功能为...

  • 3.vue计算属性和过滤器

    1.计算属性 Vue中的computed属性称为计算属性.它与methods不同,computed是响应式的,调用...

  • Vue

    computed 计算属性 computed的结果会被缓存,除非依赖的响应式属性变化才会重新计算,主要当做属性来使...

  • computed缓存 VS methods方法

    computed 计算属性【选项】 computed 属性会基于它所依赖的数据进行缓存 每个 computed 属...

  • computed && watch && methods

    computed计算属性适用于任何复杂逻辑的计算 computed 和 计算方法computed: 是基于它们的依...

  • computed、watch和methods区别

    1.computed是计算属性,依赖其他属性值,并且computed的值有缓存。只有computed依赖的属性值发...

网友评论

      本文标题:computed 计算属性

      本文链接:https://www.haomeiwen.com/subject/xzydyktx.html