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

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

無法使用 ngif 讀取 null 的屬性“nativeElement”

無法使用 ngif 讀取 null 的屬性“nativeElement”

www說 2023-08-24 18:16:55
我正在做一些單元測試。所以我有這個功能:selectCluster(event: MouseEvent, feature: any) {    event.stopPropagation();     this.selectedCluster = {geometry: feature.geometry, properties: feature.properties};  }和模板: <mgl-popup *ngIf="selectedCluster" [feature]="selectedCluster">          </mgl-popup>我有這樣的測試:  fit('Should set selectedCluster when clicked', async(() => {        spyOn(component, 'selectCluster').and.callThrough();    fixture.detectChanges();    fixture.debugElement.query(By.css('.marker-cluster')).nativeElement.click();    tick();    fixture.whenStable().then(() => {      expect(component.selectCluster).toHaveBeenCalled();    });  }));但我仍然收到此錯誤:Cannot read property 'nativeElement' of null那么我必須改變什么?謝謝這也使用了以下函數: <ng-template mglClusterPoint let-feature>        <div class="marker-cluster" (click)="selectCluster($event, feature)">          <fa-icon [icon]="faVideo" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>          <fa-icon [icon]="faWifi" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>        </div>      </ng-template>
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

.marker-cluster當您嘗試單擊它時,它不在頁面上,我沒有看到您的完整 html,但我會假設它與*ngIf="selectedCluster"真實相關。


 fit('Should set selectedCluster when clicked', async(() => {    

    // spy on and calling through doesn't actually call the function

    // it makes it so we can determine if the function was called

    // and everytime the function was called, call the actual function

    // and not a null function

    spyOn(component, 'selectCluster').and.callThrough();

    // calling the function will should make selectedCluster true

    component.selectCluster({ stopPropagation: () => null } as MouseEvent, {}); // send your own inputs

    fixture.detectChanges();

    fixture.debugElement.query(By.css('.marker-cluster')).nativeElement.click();


    fixture.whenStable().then(() => {

      expect(component.selectCluster).toHaveBeenCalled();

    });

  }));


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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