亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅在使用 VueJs 的 for 中的單個 <li> 上增加一個數字

僅在使用 VueJs 的 for 中的單個 <li> 上增加一個數字

楊__羊羊 2022-10-13 16:04:19
我希望在我的 v-for 中,當我單擊 UP 按鈕時,鏈接到該按鈕的 li 將其狀態增加 +1。這是我的一些代碼:Vue.component('app', {    data: function () {        return {            messages: '',            state: 0,            id: 0,            todo: [],            columns: ["todo", "doing", "done"],        }    },    methods: {        upElement: function (id) {            this.id            this.state++;        },        removeElement: function (key) {            this.todo.splice(key, 1);        },    },    template: `        <ul>            <li v-if="state === 0" v-for="(item, key) in todo" v-bind:messages="todo.messages + todo.state + todo.id" :key="item.id" v-bind:id="key">                <span>{{item.messages}}</span></br>                <button v-on:click="upElement">UeP</button>                <button v-on:click="removeElement(key)">remove</button>            </li>        </ul>=    `,})我想放一個 id 來定位 li 但它不起作用
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

您可以將 item 對象直接傳遞給您的upElement函數并修改 state 屬性。


Vue.component('app', {

    data: function () {

        return {

            messages: '',

            state: 0,

            id: 0,

            todo: [],

            columns: ["todo", "doing", "done"],

        }

    },


    methods: {

        upElement: function (item) {

            item.state++;

        },

        removeElement: function (key) {

            this.todo.splice(key, 1);

        },

    },


    template: `

                    <ul>

                        <li v-if="state === 0" v-for="(item, key) in todo" v-bind:messages="todo.messages + todo.state + todo.id" :key="item.id" v-bind:id="key">

                            <span>{{item.messages}}</span></br>

                            <button v-on:click="upElement(item)">UeP</button>

                            <button v-on:click="removeElement(key)">remove</button>

                        </li>

                    </ul>=

    `,

})


查看完整回答
反對 回復 2022-10-13
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號