• Vue 变异方法sort&reverse对评论进行排序


    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <script src="vue.js"></script>
        <title id="title">{{title}}</title>
    </head>
    <body>
    <div id="ask"><!--vue不能控制body和html的标签-->
        <ul>
            <li v-for="(v,k) in list">
               {{v.id}} ===== {{v.content}}<button v-on:click="remove(k)">删除</button>
            </li>
        </ul>
        <textarea v-model="content" cols="30" rows="10"></textarea><br>
        <button v-on:click="push('pre')">发表到前面</button>
        <button v-on:click="push('end')">发表到后面</button>
        <br>
        <button v-on:click="del('first')">删除第一条</button>
        <button v-on:click="del('last')">删除最后一条</button>
        <br>
        <button v-on:click="sort">排序</button>
        <button v-on:click="reverse">反转</button>
    </div>
    <script>
        var vue = function (options){new Vue(options)};
        vue({
            el:'#title',
            data:{
                title:'Vue 变异方法sort&reverse对评论进行排序'
            }
        });
        var app = vue({
            el:'#ask',
            data:{
                content:'',
                list:[
                    {'id':3,'content':'hello'},
                    {'id':5,'content':'简单记录'},
                    {'id':2,'content':'个人博客'},
                    {'id':1,'content':'学习笔记'},
                    {'id':4,'content':'想学什么学什么 '}
                ]
            },
            methods:{
                reverse(){
                    return this.list.reverse();
                },
                sort(){
                    this.list.sort(function (a,b) {
                        return a.id>b.id;
                    })
                },
                remove(k){
                    this.list.splice(k,1)
                },
                push(type){
                    var id = this.list.length+1;
                    var content_push = {id:id,'content':this.content};
                    switch (type) {
                        case 'pre':
                            this.list.unshift(content_push);
                            break;
                        case "end":
                            this.list.push(content_push);
                            break;
                    }
                    this.content='';
                },
                del(type){
                    switch (type) {
                        case 'first':
                            this.list.shift();
                            break;
                        case "last":
                            this.list.pop();
                            break;
                    }
                }
            }
        });
    
    </script>
    </body>
    </html>
  • 相关阅读:
    牛客(14)链表中倒数第k个结点
    牛客(13)调整数组顺序使奇数位于偶数前面
    牛客(12)数值的整数次方
    牛客(11)二进制中1的个数
    牛客(10)矩形覆盖
    牛客(9)变态跳台阶
    牛客(8)跳台阶
    牛客(7)斐波那契数列
    Docker数据卷
    Docker镜像
  • 原文地址:https://www.cnblogs.com/tommymarc/p/11641315.html
Copyright © 2020-2023  润新知