简体   繁体   English

如何从规范文件 angular 7 中的组件访问 window object

[英]How to access window object from component in spec file angular 7

 export class HomeComponent { constructor() { } gettableaudata(){ this.windowTableau.api= window; this.windowTab = this.windowTableau.api.tableau; return this.windowTab; } }

I have been trying to access window object of my component in my spec file, but the property( window.tableau) I am interested in is coming out to be undefined我一直在尝试在我的规范文件中访问我的组件的 window object,但是我感兴趣的属性(window.tableau)是未定义的

I tried storing the window.tableau in a local variable or a method inside a component and tried accessing in the spec file but it didn't work.我尝试将 window.tableau 存储在局部变量或组件内的方法中,并尝试在规范文件中访问,但它不起作用。

You can(or even, should) use a custom Provider for window and mock it:您可以(甚至应该)为 window 使用自定义 Provider 并模拟它:

In your TestBed:在您的测试床上:

const windowMock: Window = { 
    tableau: () => {doSomething()}
};


TestBed.configureTestingModule({
    providers: [
      ...
      { provide: 'Window', useValue: windowMock }
    ]
  });

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

相关问题 如何使用@viewChildren 在 Angular 中以 QueryList 的形式访问规范文件中的子组件属性 - How to Access child component property in spec file as QueryList in Angular using @viewChildren 从另一个模块/窗口访问Angular 2组件 - Access Angular 2 component from another module / window 将变量从Angular组件传递到window对象 - Pass variables from Angular component to window object 在组件生成时删除 nativescript angular 上的 .Spec 文件 - Removing .Spec file on nativescript angular on component generation 如何从angular 2组件访问外部javascript文件 - How to access external javascript file from angular 2 component 如何访问规范文件中的@ContentChildren? - How to access the @ContentChildren in the spec file? 如何在Angular中访问子组件对象 - How to get access to child component object in Angular 从子模态窗口访问数据到父组件angular - access data from child modal window to parent component angular 我无法从 Angular 的规范文件中访问组件 - Karma/Jasmine 单元测试 - I can't reach the component from the spec file in Angular - Karma/Jasmine unit tests 如何在关联的 spec.ts 文件中反映 Angular component.ts 的变化 - How to reflect Angular component.ts changes in associated spec.ts file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM