angular - URL 更改时组件不会加载

标签 angular angular2-routing

我正在尝试导航到同级组件,URL 更改但组件没有更改,它保留在当前组件中。我必须刷新应用程序才能加载破坏 SPA 设计的组件。 我测试了来自 Navbar 组件的相同链接,该组件也是 PlatformResult 组件的同级组件,该组件工作正常,但在尝试从 StatusTable 导航时不起作用成分。我确信我在这里做了一些愚蠢的事情,请帮我弄清楚这里发生了什么。

作为解决此问题的方法,我使用事件服务从状态组件通知应用程序组件,应用程序组件导航到 url,并且组件按预期加载。但我不喜欢这个,因为对于一个简单的问题来说这似乎有点过分了

app.component.ts

  @Component({
selector: 'app',
template: `
<div class="wrapper">
    <navbar></navbar>
    <div class="content-wrapper container-fluid"> 
       <div class="row">
       <div class="col-md-12">
        <div class="alert alert-danger" role="alert" [ngStyle]="{'display': style}">{{error}}</div>
        <router-outlet></router-outlet>
       </div>
       </div>

    </div>
   <app-footer></app-footer>
</div>
`,
directives: [Navbar, Sidebar, Footer, ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS, DataService, NavigationService],
styleUrls: ['styles/app.css']

   @RouteConfig([{
        path: '/status',
        name: 'Home',
        component: StatusTable,
        useAsDefault: true
    }, {
            path: '/platform',
            name: 'Platform',
            component: PlatformResult
        }, {
            path: '/**',
            name: 'Other',
            redirectTo: ['Home']
        }])

export class AppComponent {
    error: string;
    showError: boolean;
    style: Object;

    constructor(private navigationService: NavigationService, private router: Router) {
        // listen to navigation events
        // AS A WORKAROUND, THIS IS THE WAY I CAN NAVIGATE TO /platform from /status
        navigationService.navigationAnnounced$.subscribe(
            message => {
                router.navigate([message.componentName, message.params]);
            }
        );
    }
}

status.template.html(部分)

...  
<thead class="heading" [ngClass]="{'fade-table': spinner}">
        <tr>
            <th>Diffs ({{diffCount}})</th>
            <th *ngFor="#platform of platforms">
                <div class="vertical" style="width:30px">
                    <a [routerLink]="['/Platform', {name: platform.name}]">
                        <small>{{platform?.name}}<span class="text-danger" *ngIf="platform.ssl">({{platform?.ssl}})</span></small>
                    </a>
                </div>
            </th>
            <th><small>Assigned To</small></th>
            <th><small class="pull-left">Comments</small></th>
        </tr>
        <tr>
        </tr>
    </thead>
    ....

最佳答案

中删除ROUTER_PROVIDERS
providers: [ROUTER_PROVIDERS, DataService, NavigationService],

仅将它们添加到bootstrap(AppComponent, [ROUTER_PROVIDERS])

拥有全局实例。如果您将它们添加到组件上,组件也会获得一个不同的(新的)实例,这会破坏路由。

关于angular - URL 更改时组件不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36316432/

相关文章:

javascript - 直接以 Angular 更改构建文件是一个好习惯吗?

javascript - NgFor 过度观察未显示 NGRX 商店中的任何商品

azure - Angular2 - 在 Azure 中托管时页面刷新 404ing

angular - 使用 Angular 2 路由器的 url 自定义编码(使用 + 号代替空格)

angular - 如何使用angular2 material Dialog 来block for canDeactivate

angular - 如何在 angular2 的数组中的数组中设置补丁值(更新嵌套值)?

angular - 拦截器中的 catchError 消除了取消请求的能力

javascript - Ionic 3 多页面功能模块

使用 Indexeddb 作为离线数据存储的 Angular5 HTTP 服务 : how to handle Observable<response> vs Object

Angular 2 url路径不起作用