亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 7 - 布爾值始終為真

Angular 7 - 布爾值始終為真

精慕HU 2021-08-26 16:23:57
我在 Angular 7 中有一個布爾值,我用它在對象構造函數中設置一個值。布爾值作為參數傳遞,我將其存儲在本地。但是,由于某種原因,布爾值總是評估為真。這是有問題的代碼:  ngOnInit() {    this.id = this.route.snapshot.params['id'];    this.showPersonal = this.route.snapshot.params['showPersonal'];    console.log("ngOnInit, this.id: " +  this.id);    console.log("ngOnInit, this.showPersonal: " +  this.showPersonal);    // if showPersonal is true, workRelated i.e. last item in constructor, is false    if (this.showPersonal){      console.log("showPersonal is true, setting work related to false")      this.showWorkRelated = false;    }    else {      console.log("showPersonal is false, setting work related to true")      this.showWorkRelated = true;     }console.log("this.showWorkRelated: " + this.showWorkRelated)這是輸出:todo.component.ts:28 ngOnInit, this.showPersonal: falsetodo.component.ts:32 showPersonal is true, setting work related to falsetodo.component.ts:40 this.showWorkRelated: false我嘗試了很多不同的方法,但它總是評估為真。這段代碼有什么問題?
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

看起來您this.route.snapshot.params['showPersonal']返回一個字符串,false因此將if (this.showPersonal)其視為 true。

您應該嘗試使用typeof this.showPersonal以查看變量是哪種類型。


查看完整回答
反對 回復 2021-08-26
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號