2

I'm trying to load my Angular component with my router, but it never shows up nor throws an error.

app-routing-module

  { path: '', redirectTo: '/home', pathMatch: 'full' },
  {
    path: 'home', component: HomeComponent, children:
      [
        { path: 'compare', component: CompareComponent, children:[] },
        {
          path: 'settings', component: SettingsComponent, children: [
            { path: 'new', component: ServerConnectionEditComponent }
          ]
        },
      ]
  },

app-server-connections

  constructor(private router:Router, private route:ActivatedRoute) { }
  onAddServer()
  {
    console.log(this.route.url)
    this.router.navigate(['new'], {relativeTo: this.route});
  }

It seems the URL is valid, because any other URL throws an error.

app-server-connection-edit

  ngOnInit() {
    console.log("in Edit") //never gets here
  }

server-connections.component.html

...
<th scope="col"><button type="button" class="btn btn-success btn-sm"
                  (click)="onAddServer()">+</button></th>

app-component.html

<app-header></app-header>
<div class="container">
  <div class="row">
    <div class="col-md-12"> 
      <router-outlet></router-outlet>
    </div>
  </div>
</div>

settings.component.html

<div class="container">
  <div class="row">
    <div>
      <div class="col-md-12" >
        <h4>Server Connections:</h4>
      </div>
      <br/>
      <div class="col-xs-12">
          <app-server-connections [serverConnections]="serverConnections"></app-server-connections>
      </div>
    </div>
  </div>
</div>

4
  • Which url are you trying to navigate to? Commented Aug 11, 2019 at 10:00
  • localhost:4200/home/settings/new Commented Aug 11, 2019 at 10:02
  • Do you have enough <router-outlet>'s set up? Remember that when using children, each parent component needs a <router-outlet> in its template. Commented Aug 11, 2019 at 10:05
  • If I add <router-outlet> the component loads right away, and I want it to load on button click. Commented Aug 11, 2019 at 10:07

2 Answers 2

2

You can only have 1 level nested child in your route. So change your route like this. If this is not work open browser console to see if it there any error

{
    path: 'home', component: HomeComponent, children:
    [
        { path: 'compare', component: CompareComponent },
        {
            path: 'settings/new', component: ServerConnectionEditComponent

        },
        { path: 'settings', component: SettingsComponent }
    ]
}

And make sure add <router-outlet></router-outlet> inside your app.component.html

Sign up to request clarification or add additional context in comments.

5 Comments

Now it just loads my copmonent instead of my ServerConnectionsComponent instead of next to it.
settings.component.html <div class="container"> <div class="row"> <div> <div class="col-md-12" > <h4>Server Connections:</h4> </div> <br/> <div class="col-xs-12"> <app-server-connections [serverConnections]="serverConnections"></app-server-connections> </div> </div> </div> </div> server-connections.component.html ``` ... <th scope="col"><button type="button" class="btn btn-success btn-sm" (click)="onAddServer()">+</button></th> </tr> ```
just change the route order i just update my answer
Thanks! Much appreciated
This worked, but how do you go further levels down?
1

The more specific must come in order before the less specific. So, settings/new must be in order before settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.