繁体   English   中英

将 2 个不同的 If-Statement 读为相同的页面

[英]Page reading 2 different If-Statements as the same

我在这里遇到了一个非常令人困惑的情况:

   if ((a.length == 0 || b === null)) {
        this.noNotif = true;
        console.log("1");
      }
      if (a.length > 0 || b === null) {
        this.newNotif = true;
        // this.noNotif = false;
        console.log("2");
      } else {
        if (a.length === b.length) {
          console.log("No New Notifications");
          this.noNotif = true;
        } else {
          console.log("New notifications");
          this.newNotif = true;
        }

控制台日志记录 a.length 返回 0 并且 'b' 为 null 然而,问题是前两个 if 语句的条件不知何故都得到了满足。 noNotif 和 newNotif 都显示一个 Vue 组件,并且当前都在显示。

关于 a & b 的一些背景信息

'a' 应该是来自在页面加载时获取的 API 的数据。 'b' 应该是一个 localStorage 对象数组

第一个 if 语句处理没有 API 数据或任何存储在 LocalStorage 中的新用户

第二个 if 语句处理用户在 API 中有数据但在 LS 中没有数据的情况。

第一个嵌套的 if 语句是来自 API 的数据是否与 LS 数据匹配

嵌套的 else 语句是如果 API 数据与 LS 中的数据不同(更长)

编辑:事实证明,您也没有在两者之间添加“其他”语句。 他们都在触发,因为他们都在注册 || b = 空。

  if ((a.length == 0 || b === null)) {
    this.noNotif = true;
    console.log("1");
  } else if (a.length > 0 || b === null) {
    this.newNotif = true;
    // this.noNotif = false;
    console.log("2");
  } ...

暂无
暂无

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

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