2

I have lost over 7 days with this, but couldn't get it to work. When I put path to the template in the children, I get the error, and application crashes.

Here is my code:

export const routes: Routes = [
{path: 'store', component: StoreComponent, 
    children: [
          {path: 'apple', component: AppleComponent}
    ]
}

AppleComponent.ts

import {Component, OnInit, Type} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";

@Component ({
    selector: 'apple',
    templareUrl: '../apple.html
})

export class AppleComponent implements OnInit {
     constructor(){}

     nGOnInit():void{}
}

I have put the <base href="/"> in the index.html file but that didn't help.

I have just started learning Angular2, so please forgive me if I'm asking a stupid question.

EDIT: This would need to go in the children because of router-outlet.

Thanks guys.

1 Answer 1

1

You can simply use this

{ path: 'store/apple', component: AppleComponent, pathMatch: 'full' }

Alternatively you can use this way,

export const storeChildrenRoutes: Routes = [
  {
    path: 'store',
    component: StoreComponent,
    children: [
      {
        path: 'apple',
        component: AppleComponent,
      }
    ]
  }
];
@NgModule({
  imports: [
    RouterModule.forChild(storeChildrenRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class StoreChildRoutingModule {}

Have this as a separate module and import this to the AppRouting module. where you AppRouting module will contain your main routes which has

RouterModule.forRoot(appRootRoutes)
Sign up to request clarification or add additional context in comments.

8 Comments

Yes I can, but I need it in the children, because of router-outlet.
are you using <router-outlet> inside your StoreComponent
No, how would you do that exactly?
my question is then why you want it to be a children route of it?
Because it is the part of the wrapper. How would you put router-outlet inside?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.