【发布时间】:2018-11-30 13:36:03
【问题描述】:
我想将 titleKey 参数传递给自定义模块 navbar,该模块在延迟加载页面的 components.module.ts 中隔离。
components.module.ts
@NgModule({
declarations: [NavbarComponent],
imports: [
IonicModule,
CommonModule],
exports: [NavbarComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
navbar.component.ts
@Component({
selector: 'navbar',
templateUrl: 'navbar.html',
inputs: ['titleKey']
})
export class NavbarComponent {
@Input() titleKey:string;
constructor(private navCtrl:NavController) {
}
}
当我在延迟加载页面中使用此 navbar 时,输入不起作用
home.html
<ion-header>
<navbar titleKey="my custom title"></navbar>
</ion-header>
<ion-content>
.....
</ion-content>
如果我在navbar.component.ts 中打印@Input() titleKey:string 的结果,它是未定义的。
我已经尝试以其他形式调用此导航栏,但它似乎不起作用。
<ion-header>
<navbar [titleKey]="my custom title"></navbar>
</ion-header>
and
<ion-header>
<navbar [titleKey]="'my custom title'"></navbar>
</ion-header>
【问题讨论】:
-
如果你在组件装饰器的
inputs属性中声明你的输入,我认为你不应该在你的类成员声明中装饰它们。因此,请尝试删除组件装饰器中的inputs属性,或者在声明之前删除装饰器@Input()。 -
不确定是否已解决,但请尝试使用
@Input() public titleKey: string;。然后,在ngOnInit中,执行:console.log(this.titleKey);。还有,你为什么用inputs? -
在 ngInit 中保留 console.log(this.titleKey) 并检查。不在构造函数中
-
@Florian 我要试试
标签: javascript angular ionic3