简体   繁体   English

使用路由器出口时,使用Input属性将值从一个组件传递到另一组件

[英]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 相反,您可以选择以下选项

  • Use Route params 使用路线参数
  • Use Query params 使用查询参数
  • Shared service to share the data 共享服务共享数据
  • State Management using redux tools 使用Redux工具进行状态管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用<router-outlet>在应用程序组件以外的组件中? - Using the <router-outlet> in components other than the application component? 角度:使用相同<router-outlet>在一个组件中两次 - angular: using the same <router-outlet> twice in one component Angular2中的子级属性更改时更新父级属性(使用路由出口加载的子级组件) - Update Parent Property When Child Property Changes in Angular2 ( Child component loaded using router-outlet) Angular 2 - 从router-outlet生成的访问子组件属性 - Angular 2 - Access child component property generated from router-outlet 从同一路由器出口从另一个组件链接到一个组件 - Link to one component from another from the same router-outlet 路由器插座组件中的输入变量 - Input variable in a router-outlet component 在路由器出口内以角度2从一个组件链接到另一个组件? - Linking from one component to another within an router-outlet in angular 2? 仅使用 CSS 从路由器插座组件隐藏页脚组件? - Hiding footer component from the router-outlet component using only CSS? Angular 将 flex 布局应用于使用“router-outlet”注入的组件 - Angular applying flex layout to component that is injected using “router-outlet” 使用@Input在组件之间传递数据并使用router-out? - Passing data between components with @Input and using router-outlet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM