• 21.VUE学习之-操作data里的数组变异方法push&unshit&pop&shift的实例应用讲解


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
    <div id="hdcms">
        <li v-for="v in comments">
            {{v.content}}
        </li>
        <textarea v-model="current_content" cols="30" rows="10"></textarea><br>
        <button v-on:click="push('end')">发表到后面</button>
        <button v-on:click="push('pre')">发表到前面</button>
        <br>
        <button v-on:click="del('last')">删除最后一条评论</button>
        <button v-on:click="del('first')">删除第一条评论</button>
    </div>
    <script>
        var app = new Vue({
            el: '#hdcms',
            data: {
                //当前用户输入内容
                current_content: '',
                comments: [
                    {content: '后盾人'},
                    {content: '向军老师'},
                ]
            },
            methods: {
                push(type){
                    var content = {content: this.current_content}
                    switch (type) {
                        case 'end':
                            this.comments.push(content);
                            break;
                        case 'pre':
                            this.comments.unshift(content);
                            break;
                    }
                    this.current_content = '';
                },
                del(type){
                    switch (type) {
                        case 'last':
                            this.comments.pop();
                            break;
                        case 'first':
                            this.comments.shift();
                            break;
                    }
                }
            }
        });
    </script>
    </body>
    </html>
    

    效果:

  • 相关阅读:
    新四军的7个师,以及粟裕的山头背景
    基于easyui的webform扩展
    Mac入门(一)基本用法
    HtmlAgilityPack实战代码
    摄像头、麦克风、扬声器测试程序
    依赖注入(IOC)
    类型
    C#私房菜[二][提供编程效率的技巧]
    Fluent Nhibernate code frist简单配置
    Ubuntu环境搭建系列—JavaEE篇
  • 原文地址:https://www.cnblogs.com/haima/p/10236862.html
Copyright © 2020-2023  润新知