[英]RouterLink does not work properly
我有一個與RouterLink一起使用的應用程序。
這是App模塊中的imports數組:
imports: [
BrowserModule,
RouterModule.forRoot([
{path: '', component: HomeComponent},
{path:'contact', component: ContactComponent},
{path: 'about/:id/:name', component: AboutComponent}
])
這是app.component.html:
<div>
<a routerLink="" routerLinkActive="active">Home</a>
<a routerLink="/contact" routerLinkActive="active"> Contact</a>
</div>
<router-outlet></router-outlet>
如果用戶單擊“ 關於此用戶” ,則在主頁上應將其重定向到特定用戶的“關於”頁面:
<div>
<a [routerLink]="['/about', follower.id, follower.name]">About this user { ... </a>
</div>
但是,當應用加載時,我在控制台上收到此錯誤: 無法讀取未定義的屬性“ id”
您能幫我找出問題所在嗎?
如下使用安全導航操作員,
<a [routerLink]="['/about', follower?.id, follower?.name]">About this user { ... </a>
使用?檢查對象密鑰是否存在。 像這樣的運算符:
<div>
<a [routerLink]="['/about', follower?.id, follower?.name]">About this user { ... </a>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.