【发布时间】:2018-05-26 21:01:15
【问题描述】:
使用 stackblitz 起点,我添加了以下路由:
const routes : Routes = [ {path: 'hello', 'component': HelloComponent}];
@NgModule({
imports: [
RouterModule.forRoot(routes, {enableTracing: true}) ],
declarations: [ AppComponent, HelloComponent ],
})
还在app-component.html模板中添加了<router-outlet>,点击以下链接时会呈现hello-component:
<a routerLink="hello" routerLinkActive="active">hello</a>
但是点击链接时hello-component组件的属性为空:
@Input() name: string;
有没有办法通过模板表达式传入一个值,以便在组件上设置 name 属性,并在单击 hello 锚点的 hello-component.ts 的模板字符串中进行评估?
hello 组件如下所示,仅供参考:
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<h1>Hello {{name}}!</h1>`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent {
@Input() name: string;
}
似乎必须检查 ActivatedRoute 实例的属性才能使其正常工作?
【问题讨论】:
-
你想在哪里设置值?在路由数组中,还是要将 URL 中的值作为 URL 参数传递?目前我没有看到你在哪里传递任何价值
-
通过动态表达式中的路由器链接 .... 所以而不是 routerLink="hello" ... routerLink="此处的动态表达式 ...."
-
好的,你能把链接添加到你的 stackblitz 吗?谢谢
标签: javascript html angular angular-router