Vue 變異方法unshift&pop&shift


<!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 in list">
            {{v.content}}
        </li>
    </ul>
    <textarea v-model="content" cols="30" rows="10"></textarea>

    <button v-on:click="push('pre')">發表到前面</button>
    <button v-on:click="push('end')">發表到后面</button>
    

    <button v-on:click="del('first')">刪除第一條</button>
    <button v-on:click="del('last')">刪除最后一條</button>
</div>
<script>
    var vue = function (options){new Vue(options)};
    vue({
        el:'#title',
        data:{
            title:'Vue 變異方法unshift&pop&shift'
        }
    });
    var app = vue({
        el:'#ask',
        data:{
            content:'',
            list:[
                {'content':'hello'},
                {'content':'簡單記錄'}
            ]
        },
        methods:{
            push(type){
                var content_push = {'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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM