首先,我将DomSanitizer导入到组件中:
import { DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';之后,我创建了一个类并将它添加到构造函数中:
export class BlocklyComponent implements OnInit {
primaryWorkspace: Blockly.WorkspaceSvg;
GEE = "PCFET0NUWVBFIGh0bWw+Cgo8aGVhZD4KPG1ldGEgY29udGVudD0idGV4dC9odG1sOyBjaGFyc...";
public geeMap: SafeResourceUrl;
constructor(private sanitizer: DomSanitizer) {}
ngOnInit() {但是,在NgOnInit函数内部,它正确地工作在updateURL函数中(在NgOnInit之外,但在类中),它说this.sanitizer是未定义的:
//Creates de src that has to be in the iframe. What I need to do is to update this safeResourceUrl.
this.geeMap = this.sanitizer.bypassSecurityTrustResourceUrl(`data:text/html;base64,${this.GEE}`);
//Reads the events in a workspace. When it notices a change, it triggers the updateURL function
primaryWorkspace.addChangeListener(this.updateURL);
}
updateURL(primaryEvent)
{
if (primaryEvent.isUiEvent) {
return; //Do not clone ui events
};
console.log('hola');
//Problem. Here it sais that this.sanitizer is undefined.
this.geeMap = this.sanitizer.bypassSecurityTrustResourceUrl(`data:text/html;base64,${this.GEE}`);
}我知道这是我经常遇到的一个典型问题,但是现在我不知道怎么解决了。我试图在不同的论坛寻找未知的问题,但我仍然无法解决它。非常感谢
发布于 2021-03-09 11:14:46
由于要退出绑定事件的Angular流,可以通过两种方式添加事件侦听器,以处理正确的this :-
Arrow函数() :-**
primaryWorkspace.addChangeListener((event)=>this.updateURL(event));
.bind (inside constructor**) :-** )
this.updateURL = this.updateURL.bind(this);
在ngOnInit中:-
primaryWorkspace.addChangeListener(this.updateURL);
另外,我认为你指的是addEventListener('change',...(而不是addChangeListener )。
https://stackoverflow.com/questions/66545750
复制相似问题