在vue中将方法调用到另一个方法中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在vue中将方法调用到另一个方法中相关的知识,希望对你有一定的参考价值。

我正试图从vue中的另一个方法中调用一个方法。

我得到的是我的控制台中的未定义,但我真正想要的是在getId函数中调用的id

总的来说,我要做的是使用addEvent函数获取复选框事件,以便我可以从中获取true或false,然后将其发送到saveCheckbox函数,并从saveCheckbox函数调用getId函数获取特定复选框的ID。

我希望我能够正确解释它。如果还不清楚请告诉我。

这就是我所拥有的

<template>
   <div class="card-body">
       <table class="table">
           <thead class="thead-dark">
               <tr>
                   <th scope="col">Active</th>
                    <th scope="col">Title</th>
               </tr>
           </thead>

           <tbody>
               <tr v-for="(category, index) in categories" >
                    <td>
                        <input name="active" type="checkbox" v-model="category.active" @change="getId(category.id)" @click="addEvent">
                    </td>

                    <td>
                        {{ category.title }}
                    </td>
               </tr>
           </tbody>
       </table>
   </div>
</template>

<script>

    export default {

        props: [
            'attributes'

        ],

        data(){
            return {
                categories: this.attributes,
            }
        },

        methods: {
            getId(id){

                console.log(id);

                return id
            },

            saveCheckbox(event){

                console.log(this.getId());

            },

            addEvent ({ type, target }) {
              const event = {
                  type,
                  isCheckbox: target.type === 'checkbox',
                  target: {
                    value: target.value,
                    checked: target.checked
                  }
              }

                this.saveCheckbox(event.target.checked)

            }
        },

        mounted() {
            console.log('Component mounted.')
        }
    }
</script>
答案

您必须将参数(Id)传递给getId方法

另一答案

有一个排序概述,你没有将任何Id传递给方法,它试图返回该id。也许,这是没有定义的?

调用方法做得很好。有了这个。之前的关键字

以上是关于在vue中将方法调用到另一个方法中的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击适配器类中代码的项目中的删除按钮来删除列表视图中的项目后重新加载片段?

Java中代码块执行顺序

onDetach()不要求从一个片段移动到另一个片段

Quartus中代码字体大小的调整方法

Lesson Java中代码块和静态代码块的用法

vue(element)中代码高亮插件全面对比