如何从angular4中的另一个兄弟传递id到兄弟组件?

时间:2018-03-26 11:14:17

标签: angular angular-routing

以下是我的export const Approute: Routes = [ { path: 'login', component: LoginComponent }, { path: 'home', component: HomeComponent, children: [ { path: 'dashboard', component: DashboardComponent }, { path: 'trip-details/:id', component: TripDetailsComponent }, { path: 'create-expense/:id', component: CreateExpenseComponent } ] }, { path: 'not-found', component : NotFoundComponent }, { path: '**', redirectTo: '/not-found' } ];

tripDetails.component.html

<button class="btn btn-default pull-right" [routerLink]="['../create-expense', tripId]"><i class="fa fa-inr" aria-hidden="true"></i> Create Expense</button>

CreateExpenseComponent

上面我有2个兄弟组件,并尝试使用id, but am not able to navigate to http://localhost:4200/home/create-expense its redirecting me to http://localhost:4200/not-found`导航到另一个兄弟组件switch (action.type) { case UserActions.INCREMENT_ID: { const users = state.users; if (users && users.length > 0) { users[0].id += 1; } // you could also just return the state because the reference to users stays the same return { ...state, users: users } } }

哪里出错?

1 个答案:

答案 0 :(得分:0)

你应该像这样做路由:[routerLink]="['home', 'create-expense', tripId]

另外,请确保在当前组件中定义了tripId。 你可以这样得到它(在将路径注入组件的构造函数之后):

 this.route
      .params
      .subscribe(params => {
        this.tripId = params['id'];
      });