Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时调用两次Vue方法_Javascript_Vue.js - Fatal编程技术网

Javascript 单击按钮时调用两次Vue方法

Javascript 单击按钮时调用两次Vue方法,javascript,vue.js,Javascript,Vue.js,我试图使用Vue输入来记录一些简单的数据,但由于某种原因,按钮调用的方法add()会被调用两次 <el-steps :active="addItem.count" finish-status="success" class="form-items" style="padding-left: 0px"> <el-step title="category"></el-step> <el-step title="subc

我试图使用Vue输入来记录一些简单的数据,但由于某种原因,按钮调用的方法
add()
会被调用两次

    <el-steps :active="addItem.count" finish-status="success" class="form-items" style="padding-left: 0px">
        <el-step title="category"></el-step>
        <el-step title="subcategory"></el-step>
        <el-step title="data"></el-step>
    </el-steps>   
    <el-form ref="form" :model="addItem" label-width="120px" style="padding: 20px;">
        <el-input :placeholder="placeholder" v-model="addItem.text"></el-input>
        <div id="add-item-buttons">
        <el-form-item  class="form-items">
            <el-button type="primary" @click="add">Create</el-button>
            <el-button>Clear</el-button>
        </el-form-item>
        </div>
    </el-form>  

编辑:原来我打了个错字,在我的第二个开关案例中写了“类别设置为”,我的意思是“子类别”。显然,案例1和案例2都在评估中,而不是案例1两次。以下是修复后的新控制台日志:

category set to: category 1
(unknown) subcategory set to: 
而不是切换添加您的子类别,它直接进入项目


有人知道出了什么问题吗?

您的switch case语句缺少中断

将计数器增加1,第二个条件也匹配。只要加上休息,你就会好的

switch(this.addItem.count){
     case 1:
         this.addItem.category = this.addItem.text;
         console.log('category set to: ' + this.addItem.category);
         this.addItem.text = '';
         this.addItem.count++;
         break;

     case 2:
         this.addItem.subcategory = this.addItem.text;
         console.log('category set to: ' + this.addItem.subcategory);
         this.addItem.text = '';
         this.addItem.count++;
         break;
     case 3:
         if (this.addItem.kks.show){
             this.addItem.kks.name = this.addItem.text;
         }
         if (this.addItem.document.show){
             this.addItem.document.name = this.addItem.text;
         }
         if (this.addItem.product.show){
             this.addItem.product.name = this.addItem.text;
         }
         break;
}

谢谢!我想我需要复习一下switch语句。我很高兴能帮上忙。
category set to: category 1
(unknown) subcategory set to: 
switch(this.addItem.count){
     case 1:
         this.addItem.category = this.addItem.text;
         console.log('category set to: ' + this.addItem.category);
         this.addItem.text = '';
         this.addItem.count++;
         break;

     case 2:
         this.addItem.subcategory = this.addItem.text;
         console.log('category set to: ' + this.addItem.subcategory);
         this.addItem.text = '';
         this.addItem.count++;
         break;
     case 3:
         if (this.addItem.kks.show){
             this.addItem.kks.name = this.addItem.text;
         }
         if (this.addItem.document.show){
             this.addItem.document.name = this.addItem.text;
         }
         if (this.addItem.product.show){
             this.addItem.product.name = this.addItem.text;
         }
         break;
}