Angular 将值从typescript传递到routerlink
我的typescript文件中有一个属性 subject.component.tsAngular 将值从typescript传递到routerlink,angular,typescript,routerlink,Angular,Typescript,Routerlink,我的typescript文件中有一个属性 subject.component.ts subject: string; this.subject = "angular"; 我想在我的HTML文件中将其传递给routerlink subject.component.html [routerLink]="[subject,'area']" app.routes.ts path: 'angular', component: SubjectComponent, children: [
subject: string;
this.subject = "angular";
我想在我的HTML文件中将其传递给routerlink
subject.component.html
[routerLink]="[subject,'area']"
app.routes.ts
path: 'angular',
component: SubjectComponent,
children: [
{
path: 'area',
component: AreaComponent,
data: { subject: 'angular' }
}
path: 'angular2',
component: SubjectComponent,
children: [
{
path: 'area',
component: AreaComponent,
data: { subject: 'angular2' }
}
但我有问题,因为它没有解决。
请求的页面未被返回。我被重新引导到主屏幕您可以如下设置查询参数和片段:
// subject = 'sss' ; set in your component otherwise it wont be added.
请参见演示
比较这段代码,找出你做错了什么
我在app.component.html中添加了
<router-outlet></router-outlet>
<a [routerLink]="[subject,'area']"> Redirect</a>
在app.module.ts中
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
subject:string;
ngOnInit(){
this.subject = 'test';
}
}
const routes: Routes = [
{ path: 'test/area', component: HelloComponent }
]
@NgModule({
imports: [BrowserModule, FormsModule, RouterModule.forRoot(routes)],
declarations: [AppComponent, HelloComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
我遇到的问题与我试图访问的链接的相对路径有关。我所做的更改是添加了一个前导正斜杠
subject.component.html
[routerLink]=“['/',主题,'area']”“您有什么问题?请求的页面未返回。我被重新引导到主屏幕。您可以发布您的TS和HTML吗?因为您没有为主题属性分配任何内容。。因此它将返回undefinedYou没有声明可以处理此路径的路由。请提供您的路线。