简体   繁体   English

为什么这个回调中的条件总是返回false?

[英]Why is the condition in this callback always returns false?

I have a home SPA based on Vue.我有一个基于 Vue 的家庭 SPA。 One of the components is driven by a v-if="isDisplayed ".其中一个组件由v-if="isDisplayed " 驱动。

This isDisplayed is set by listening to a MQTT topic (see footnote) and new messages received are handled by the following function (I sepcifically used 'hello' instead of false to make sure the switch goes there).这是通过监听 MQTT 主题(见脚注)设置的,收到的新消息由以下isDisplayed处理(我特别使用'hello'而不是false以确保切换到那里)。 The topic of interest is display_school_edt .感兴趣的topicdisplay_school_edt

mqttMessage(topic, message) {
      console.log(`App.vue received topic ${topic} with payload '${message}'`)
      if (topic === "dash/reload") {
        window.location.href = window.location.href
        document.location.reload(true);
      }
      if (topic === "dash/darkmode") {
        this.nightmode = JSON.parse(message) ? "night" : "day";
      }
      // this is the part I have problems with, I left everything for completness
      if (topic === "display_school_edt") {
        console.log(`edt display received: '${message}'`);
        if (message === 'on') {
          this.isEdtDisplayed = true
        } else {
          this.isEdtDisplayed = 'hello'
        }
        // I initially went for the ternary below - same results
        // message === "on" ? this.isEdtDisplayed = true : this.isEdtDisplayed = 'hello';
        console.log(`new edt display: ${this.isEdtDisplayed}`);
      }
    }

When I publish to the monitored topic display_school_edt (twice: one the message is on and the other time off ), here is what I get on the console:当我发布到受监控的主题display_school_edt (两次:一个消息是on另一个 time off )时,这是我在控制台上得到的:

在此处输入图像描述

In other words, no matter if on or off is received, the condition is always false.换言之,无论接收到on还是off ,条件始终为 false。

There is something obviously wrong with my code but the more I look, the better it looks.我的代码明显有问题,但我看的越多,它看起来就越好。


Footnote: the fact that it is that specific protocol does not matter (it is a kind of bus often used with IoTs), you can assume that somehow mqttMessage() is executed with the parameters topic and message that are both strings.脚注:事实上,特定协议无关紧要(它是一种经常与物联网一起使用的总线),您可以假设mqttMessage()以某种方式执行,参数topicmessage都是字符串。

This is indeed unexpected if message is of type string.如果message是字符串类型,这确实是出乎意料的。 However, it probably is not, and the only times you output message , you actually coerce it to string.但是,它可能不是,并且只有在您 output message时,您实际上将其强制为字符串。 So if you see from a previous output that it coerces to "no", then in the if condition you should do the same, and force that conversion to string:因此,如果您从以前的 output 中看到它强制为“否”,那么在if条件下您应该执行相同的操作,并强制转换为字符串:

if (message+'' === 'no')

NB: This will call message.toString() , just like it does when you reference it within a template literal as ${message} .注意:这将调用message.toString() ,就像您在模板文字中将其引用为${message}时所做的那样。

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

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