[英]Passing value from one component to other component using Input property when using router-outlet
I have my parent component where the is defined to display other component(childComponent) when the route changes, I want to pass a value from parentComp to childComp, usually we include the selector in place of router-outlet if we don't use routes and pass using @Input property. 我有我的父组件,其中的定义为当路由更改时显示其他组件(childComponent),我想将值从parentComp传递给childComp,通常,如果我们不使用路由,我们会在选择路由器出口时包含选择器并使用@Input属性传递。 How can I achieve the same when using routes
使用路线时如何取得相同的效果
ParentComponent 为父级
@Component({
selector : 'parent',
template : `<h1>Home</h1>
<router-outlet test1="test"></router-outlet>
`,
})
export class parentComp{
test = "testing";
}
ChildCompoent ChildCompoent
@Component({
selector : 'child',
template : `<h2>Home</h2>
`,
})
export class childComp implements onChanges, onInit{
@Input() test1;
ngOnChanges(){
console.log(this.test1);
}
ngOnInit(){
console.log(this.test1);
}
}
I want to get the value of test1 from parent to child component and print in console using @Input property. 我想从父组件到子组件获取test1的值,并使用@Input属性在控制台中打印。
I'm getting the value of test1 as undefined when printing the value in console 在控制台中打印值时,我得到的test1值未定义
You cannot define an @Input()
variable for a router-outlet
. 您不能为
router-outlet
定义@Input()
变量。
Instead you have the following as alternative options 相反,您可以选择以下选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.