最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 4: changing url, but component is not rendered - Stack Overflow

programmeradmin2浏览0评论

I'm trying to link a component from one component using routerLink = "selected"

 const routes: Routes = [
      {
        path: '',
        children: [
          {
            path: 'account',
            component: AccountComponent,
            children: [
              {
                path: 'selected',
                component: SelectedComponent,
              },
            ],
          },

        ]
      }
    ];

    @NgModule({
      imports: [RouterModule.forChild(routes)],
      exports: [RouterModule],
    })
    export class AccountSettingsRoutingModule { }

This is AccountComponent

    import { Component, OnInit, AfterViewInit } from '@angular/core';
    import { Router, ActivatedRoute, RouterModule } from '@angular/router';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Http, Response, Headers } from '@angular/http';
    @Component({
      selector: 'app-list-accounts',
      templateUrl: './accounts-listponent.html',
      styleUrls: ['./accounts-listponent.scss']
    })
    export class AccountComponent implements OnInit {

      constructor(private http: HttpClient, private router: Router) { }
     ngOnInit() {}
}

The url is changing to the desired like this dashboard/account/selected, but the view is not loading.

I'm trying to link a component from one component using routerLink = "selected"

 const routes: Routes = [
      {
        path: '',
        children: [
          {
            path: 'account',
            component: AccountComponent,
            children: [
              {
                path: 'selected',
                component: SelectedComponent,
              },
            ],
          },

        ]
      }
    ];

    @NgModule({
      imports: [RouterModule.forChild(routes)],
      exports: [RouterModule],
    })
    export class AccountSettingsRoutingModule { }

This is AccountComponent

    import { Component, OnInit, AfterViewInit } from '@angular/core';
    import { Router, ActivatedRoute, RouterModule } from '@angular/router';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Http, Response, Headers } from '@angular/http';
    @Component({
      selector: 'app-list-accounts',
      templateUrl: './accounts-list.component.html',
      styleUrls: ['./accounts-list.component.scss']
    })
    export class AccountComponent implements OnInit {

      constructor(private http: HttpClient, private router: Router) { }
     ngOnInit() {}
}

The url is changing to the desired like this dashboard/account/selected, but the view is not loading.

Share Improve this question edited Jan 14, 2019 at 19:48 Liam 29.7k28 gold badges137 silver badges200 bronze badges asked Apr 9, 2018 at 19:29 yeryer 1,5322 gold badges19 silver badges33 bronze badges 3
  • Please, Share your html and component – Hitesh Kansagara Commented Apr 9, 2018 at 19:39
  • Have you added <router-outlet></router-outlet> in app.component.html? – Hitesh Kansagara Commented Apr 9, 2018 at 20:49
  • yes, it works now. Thanks – yer Commented Apr 9, 2018 at 20:52
Add a comment  | 

1 Answer 1

Reset to default 26

Add <router-outlet></router-outlet> to AccountComponent. Read more in the docs.

发布评论

评论列表(0)

  1. 暂无评论