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

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

無法在“窗口”上執行“getComputedStyle”:參數 1 不是“元素”類型。

無法在“窗口”上執行“getComputedStyle”:參數 1 不是“元素”類型。

牛魔王的故事 2022-10-27 14:51:59
我一直在 StackOverflow 和 Google 中尋找一段時間,但無法真正找到解決我的問題的方法。就這樣吧。我通過@ViewChildren('labelSquare') public labelIcon: QueryList<HTMLSpanElement>;裝飾器在 DOM 中有一組元素。在 HTML 中,我有以下內容來綁定它: <span class="status-item" *ngFor="let status of statusList">     <div *ngIf="status.state !== 'Starting' &&                 status.state !== 'Stopping' &&                 status.state !== 'Synchronising' &&                 status.state !== 'EmergencyStop'"     >       <div class="status">         <span #labelSquare class="status-square fas fa-square {{ status.state }}"></span>         <span class="status-name">{{ status.text }}</span>       </div>     </div> </span>我從中得到了一個包含 58 個跨度元素的數組,現在想要附加一個比當前背景顏色深 10% 的邊框。因此,我map為此使用了一個:if (this.labelIcon) {     this.labelIcon.map((icon: HTMLSpanElement) => {         const element = window.getComputedStyle(icon);         icon.setAttribute('style', `border: 1px solid ${ColorUtil.darkenBorderFromBackground(element.color)}`);     }); }我的ColorUtil.darkenBorderFromBackground()簡單回歸return darken(${backgroundColor}, 10%);(使用模板字符串,無法弄清楚如何在 StackOverflow 中格式化。我的問題是現在我得到了一個TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.誰能幫我嗎?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

Angular 返回一個ElementRef非 DOM 元素。

ElementRef有一個稱為nativeElementDOM 元素的屬性。所以icon改變icon.nativeElement內部window.getComputedStyle()

請注意,您的打字稿界面也需要在 map 方法內進行更改。

例如

if (this.labelIcon) {

 this.labelIcon.map((icon: ElementRef) => { // Not sure if ElementRef is a valid interface in Angular

     const element = window.getComputedStyle(icon.nativeElement);

     icon.setAttribute('style', `border: 1px solid ${ColorUtil.darkenBorderFromBackground(element.color)}`);

 });

}


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 592 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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