You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单元测试报错:sanitazer.bypassSecurityTrustStyle 不是函数求排查

解决TypeError: this.sanitazer.bypassSecurityTrustStyle is not a function问题
  • 检查拼写错误:错误信息里的sanitazer是拼写错误,正确应为sanitizer。打开你的组件代码,查找所有this.sanitazer的写法,全部替换成this.sanitizer,这是触发该错误的最常见原因。

  • 确认测试模块的DomSanitizer配置:确保你的测试模块已正确引入BrowserModule或显式提供DomSanitizer,示例配置如下:

beforeEach(async () => {
  await TestBed.configureTestingModule({
    imports: [BrowserModule],
    declarations: [YourTargetComponent],
    providers: [DomSanitizer]
  }).compileComponents();
});
  • 验证组件的DomSanitizer注入:确保组件构造函数中正确注入了DomSanitizer,示例代码:
constructor(private sanitizer: DomSanitizer) {}

你的测试代码本身逻辑没问题,但组件内部的sanitizeStyle方法如果使用了错误拼写的sanitazer调用,就会导致该错误。优先排查拼写问题,再检查依赖配置。

内容的提问来源于stack exchange,提问作者Kenzo_Gilead

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:34:58