javascript - v-for中無法取得push進來的資料?
PHPz
PHPz 2017-05-19 10:21:07
0
3
798

下段程式碼中點選button後,app.items2數組內容發生變化,第二個ul裡也被渲染進一個li,但是items2內的item.message 缺沒有渲染出來,請問是為什麼,怎麼解決?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>

    </style>
</head>
<body>
    <p id="example-1">
        <ul>
            <li v-for="item in items">
                {{ item.message }}
            </li>
            
        </ul>
        <ul>
            <li v-for="item in items2">{{ item.message }}</li>
        </ul>
        <button @click="aaa">移动</button>
    </p>
    <script src="http://vuejs.org/js/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#example-1',
            data: {
                items: [{
                    message: 'Foo'
                }, {
                    message: 'Bar'
                }],
                items2: []
            },
            methods: {
                aaa: function() {
                    this.items2.push(this.items.splice(0, 1));
                }
            }
        })
    </script>
</body>
</html>
PHPz
PHPz

学习是最好的投资!

全部回覆(3)
伊谢尔伦

Array.prototype.splice() 傳回值 由被刪除的元素組成的一個陣列。如果只刪除了一個元素,則傳回只包含一個元素的陣列。如果沒有刪除元素,則傳回空數組。

https://developer.mozilla.org...

this.items2.push(this.items.splice(0, 1)[0]);

https://jsfiddle.net/ycloud/n...

给我你的怀抱

this.items2.push(...this.items.splice(0, 1));

黄舟

splice方法回傳一個陣列
push接受不定長參數
可以使用concat方法
this.items2 = this.items2.concat(this.items.splice(0, 1));

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板