Angular 13传递参数OnClick,使用

发布于 2025-01-25 15:57:20 字数 332 浏览 1 评论 0原文

我有一系列对象。我迭代的是,循环并将每个项目的名称传递给针对函数导航(url:string)的OnClick

{{{sousmenu.titre}}

compenent:

导航(url:string):void { this.router.navigate([url]); }

错误消息是: 类型'字符串的参数|未定义的“无法分配到类型“字符串”的参数。 类型“未定义”不能分配给'string'.ngtsc(2345)

I have an array of objects. i am iterating that in loop and passing each item's name to onclick which targets a function navigate(url:string) in cmpenent.ts

html :

{{sousMenu.titre}}

compenent:

navigate(url:string):void{
this.router.navigate( [url]); }

the error message is :
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.ngtsc(2345)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2025-02-01 15:57:20

您可以提供一个完整的代码示例,以便我们可以在总上下文上提供建议。

有时,如果属性是用一种'字符串|的类型定义的当功能仅呼叫“字符串”时,需要未定义的“额外处理”。

您可以做的某些事情如下:

navigate(url:string | undefined):void{ 
this.router.navigate( [url as string]); 
}

这可能不是最好的路线,但没有进一步查看代码,很难看到我们如何扩展它。

Can you provide a complete code example so we can advise on the total context.

Sometimes if a property is defined with a type of 'string | undefined' additional handling is needed when the function calls for only a 'string'.

Some of the things you can do are the following:

navigate(url:string | undefined):void{ 
this.router.navigate( [url as string]); 
}

That might not be the best route but without further viewing of the code it's hard to see how we may expand on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文