首页 > 解决方案 > 错误类型错误:无法读取 null 的属性“替换”(无限循环)

问题描述

问题是当该字段为空时,它处于无限循环中。我无法确定如何修复此错误。这个问题只提供一种特定的服务。

错误:

ItemDetalheComponent.html: 54 ERROR TypeError: Cannot read property 'replace' of null
at ItemDetalheComponent.linkItens (item-detail.component.ts: 60)
at Object.eval [as updateRenderer] (ItemDetalheComponent.html: 54)
at Object.debugUpdateRenderer [as updateRenderer] (core.js: 45294)
at checkAndUpdateView (core.js: 44277)
at callViewAction (core.js: 44637)
at execEmbeddedViewsAction (core.js: 44594)
at checkAndUpdateView (core.js: 44272)
at callViewAction (core.js: 44637)
at execEmbeddedViewsAction (core.js: 44594)
at checkAndUpdateView (core.js: 44272)

这是第 60 行

linkItens(numeroItens: string): string {
    const numeroItensTratados = numeroItens.replace(/[^\d]+/g, '');
    return `https://teste/#/resultado/${numeros}`;
}

我的想法是从 null 更改为空,也许这将停止带来这个错误。如果有人可以帮助我至少走正确的路,我将不胜感激。

标签: angulartypescript

解决方案


numeroItens如果为空,则使用空字符串

const numeroItensTratados = (numeroItens || '').replace(/[^\d]+/g, '');

推荐阅读