简体   繁体   English

条件语句不适用于VueJs方法

[英]Conditional statement not working on a VueJs method

I'm trying to add conditional statement, so when the user toggle the buttons it would trigger an action, for example output the some text. 我正在尝试添加条件语句,因此当用户切换按钮时会触发一个动作,例如输出一些文本。

I tried to add the conditional as method, and as computed property without success, also I tried with switch statement. 我试图添加条件as方法,并作为计算属性没有成功,我也尝试使用switch语句。

I'll add the codepen link https://codepen.io/manosx/pen/KELmpj?editors=0010 我将添加codepen链接https://codepen.io/manosx/pen/KELmpj?editors=0010

  clickedTag: function (indexTag) {
      // toggle the active class
      this.$set(this.isClickedTag, indexTag, !this.isClickedTag[indexTag])
      let tagsSelected = _.keys(_.pickBy(this.isClickedTag, _.identity))
      let tagsSelectedSingle = tagsSelected.map(s => `'${s}'`).join(', ')
      console.log(tagsSelectedSingle)
      if (tagsSelectedSingle === '0') { console.log('naylon') }
      else if (tagsSelectedSingle === '1') { console.log('espiga') }
      else if (tagsSelectedSingle === '2') { console.log('omega') }
      else if (tagsSelectedSingle === '3') { console.log('crochet') }
      else if (tagsSelectedSingle === '4') { console.log('thread') }
      else if (tagsSelectedSingle === '5') { console.log('bordado') }
    },

I would like to add a conditional statement that would trigger different actions depending of the buttons that are on. 我想添加一个条件语句,根据所打开的按钮触发不同的操作。

Its beter to use indexOf() because includes will not work on some browsers. 它更好地使用indexOf()因为包含在某些浏览器上不起作用。

Try this. 尝试这个。

if (tagsSelectedSingle.indexOf('0')>=0) { console.log('naylon') }
           if (tagsSelectedSingle.indexOf('1')>=0) { console.log('espiga') }
           if (tagsSelectedSingle.indexOf('2')>=0) { console.log('omega') }
           if (tagsSelectedSingle.indexOf('3')>=0) { console.log('crochet') }
           if (tagsSelectedSingle.indexOf('4')>=0) { console.log('thread') }
           if (tagsSelectedSingle.indexOf('5')>=0) { console.log('bordado') } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM