简体   繁体   English

从子组件路由到父路由器出口

[英]Routing to parent's router-outlet from a child component

I've setup a plnkr to demonstrate what I am trying to ask 我已经设置了一个plnkr来演示我想要问的问题

I have some components in a web app, 我在网络应用程序中有一些组件,

the parent component, App 父组件, App

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div class="main-container">
      <h2>This is the base. This div must be seen on app pages</h2>
      <a [routerLink]="['Home']"> Home </a> | 
      <a [routerLink]="['Users']"> Users  </a> | 
      <a [routerLink]="['Contact']"> Contact </a>
    </div>

    <router-outlet></router-outlet>
  `,
  directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
  {
    path: '/',
    name: 'Home',
    component: HomeComponent,
    useAsDefault: true

  },
  {
    path: '/users/...',
    name: 'Users',
    component: UsersComponent
  },
  {
    path: '/contact',
    name: 'Contact',
    component: ContactComponent
  }
])
export class App {

}

UsersComponent is loaded to the router-outlet of the parent controller at the route /users which is shown below, UsersComponent被加载到路由/users的父控制器的router-outlet ,如下所示,

@Component({
  selector: 'my-app',
  template : `

    <div  *ngFor="#page of pages">
      <a [routerLink]="[page.name]">
        {{page.slug}}
      </a>
    </div>

    <router-outlet></router-outlet>
  `
})
@RouteConfig([
  {path : '/', name: 'Main', component: MainRoute, useAsDefault: true}  
])
export class UsersComponent{
  /**
  * some methods to configure the routes,
  */
}

UsersComponent has dynamically configured routes for users, UsersComponent已为用户动态配置路由,

eg. 例如。 /alex , /john etc, /alex/john等,

since the routes are from the child component, the path looks like example.com/users/alex , example.com/users/john etc.. But I want the path to be example.com/alex and example.com/john 由于路线是从子组件,该路径看起来像example.com/users/alexexample.com/users/john等。但我想要的路径是example.com/alexexample.com/john

also I want to load the component associated with each user to the parent's router-outlet . 我还想将与每个用户关联的组件加载到父router-outlet In the example here, to the router-outlet of App so that I can get rid of the user list. 在这里的示例中,到Approuter-outlet ,以便我可以摆脱用户列表。

Thank You for any help. 感谢您的任何帮助。

Here is my take on your configuration: Plunker 以下是我对您配置的看法Plunker

1. Define your App 's RouteConfig like this 1.像这样定义你的AppRouteConfig

@RouteConfig([
  {
    path: '/',
    name: 'Home',
    component: HomeComponent,
    useAsDefault: true

  },
  {
    path: '/:user',
    name: 'User',
    component: PersonComponent
  },
  {
    path: '/users',
    name: 'Users',
    component: UsersComponent
  },
  {
    path: '/contact',
    name: 'Contact',
    component: ContactComponent
  }
])

2. Remove router.config and <router-outlet> from your UsersComponent 2.删除router.config<router-outlet>UsersComponent

Don't send the user data via RouteData because than you'll have to define routes for every user in advance, which is not necessary/ efficient. 不要通过RouteData发送用户数据,因为您必须提前为每个用户定义路由,这不是必需/有效的。

@Component({
  selector: 'my-app',
  template : `

    <div  *ngFor="#page of pages">
      <a [routerLink]="['User', {user: page.name}]">
        {{page.slug}}
      </a>
    </div>
  `,
  providers : [PagesService]
})
export class UsersComponent{
  pages = [];

  constructor(private pgSvc: PagesService, router: Router) {
  }
}

3. Structure your service to send data for a specific user 3.构建服务以为特定用户发送数据

4. Update your PersonComponent to accomodate with these changes. 4.更新PersonComponent以适应这些更改。

@Component({
  selector : 'person',
  template : `
  <div class="person-container">
    <h3>Person selected : <strong> {{data.get['user']}} </strong></h3>
  </div>
  `
})
class PersonComponent {
  constructor(public data: RouteParams) {

   // call your service here to get the user specific data by using 
    let userName = data.get('user');

   // yourService.getUserData(userName);
  }
  routerCanReuse(next, prev) {
    return false;
  }
}

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

相关问题 Angular 5从子插座在父路由器出口上触发导航 - Angular 5 trigger navigation on parent router-outlet from child outlet 将数据从路由器出口中的组件传递到“父”组件[Angular 2] - Pass data from component in router-outlet to a "Parent'' component [Angular 2] Angular 2-将数据从子级(在路由器出口内)传递到父级组件 - Angular 2 - Passing data from child (within router-outlet) to parent component 从父级调用子级方法(并传递数据),子级将在 angular 中动态加载组件(路由器出口或延迟加载子级) - Invoke Child method(and pass data) from parent, Child will be dynamically loaded component(router-outlet or lazy loaded child) in angular 导航a <router-outlet> 来自其父母 - Navigating a <router-outlet> from its parent 子模块中的Angular 6 routerlink和父模块中的router-out(app.component) - Angular 6 routerlink in child module and router-outlet in parent (app.component) 通过路由器出口“子”组件传递父组件变量 - Pass a parentcomponent variable trough the router-outlet “child” component 如何从父组件调用路由器出口子组件方法 - How to call router outlet child component method from parent comonent 路由器插座不工作我的组件 - router-outlet not working my component Angular2组件与路由器插座通信 - Angular2 component communication with router-outlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM