[Vuejs]-First tab to be selected by default

0👍

Change the logic behind the calculation of the index:

        calcFileIndex () {
          return this.selectedFile === 1    
                         ?    1
                         :    this.selectedFiles.findIndex(({ name }) => name === this.selectedFile.name);
        }

Then in your watcher, validate the index:

        selectedFile () {
          let index = this.calcFileIndex()
          if(index>0) 
              this.tab = index;
          else 
              this.tab = 1;
        }

Also, set the props better, if it is numeric, then put the type as numeric and validate that it is greater than zero:

props:{
    selectedFile:{
       type:Number,
       default:1,
       validator:function(value){return value>0;}
    }
}

You can check more types at the docs, to avoid weird errors.

Leave a comment