我为路由模块创建了一个matcher函数,但出现一个奇怪的错误:即使我检查对象是否存在,也无法读取null的属性“ path”。
export function contentMatcher(url) {
//for simplifying, assume that all checks returned true
return {
consumed: url,
posParams: {
type: new UrlSegment(url[0].path, {}),
item: url[1] ? new UrlSegment(url[1].path, {}) : null //<------ Cannot read property 'path' of null
}
}
}
const routes: Routes = [
{
matcher: contentMatcher,
component: ContentComponent
}
]
我知道我必须导出匹配器函数,因为aot不支持函数表达式。 但我不知道posParams是否也需要它,即使如此,如何将“ url”传递给它。
已解决
通过替换item: url[1] ? new UrlSegment(url[1].path, {}) : null
解决了该问题
与item: new UrlSegment(url[1] ?url[1].path: null, {})