2017-02-16 115 views
0

我不知道如何在从AngularJS升级到ng2的应用中执行通配符路径路由。混合AngularJS和Angular 2路由时的通配符路径

一般来说,混合的路由这样的两种:

的第一步具有双重路由器的设置是添加包含每个路由器一个出口的角根 组件。 AngularJS将使用ng-view的 ,而Angular将使用router-outlet。当用户使用它的 路由器时,另一个插座将是空的。

import { Component } from '@angular/core'; 
@Component({ 
    selector: 'my-app', 
    template: ` 
    <router-outlet></router-outlet> 
    <div ng-view></div> 
    `, 
}) 
export class AppComponent { } 

https://angular.io/docs/ts/latest/guide/upgrade.html#!#dividing-routes-between-angular-and-angularjs

在角2可以指定这样一个通配符路径:

{路径: '**',组分:PageNotFoundComponent}

在AngularJS可以有这样的通配符路径:

$routeProvider 
    .otherwise({ 
    template: '<page-not-found></page-not-found>', 
    }); 

当我把所有东西放在一起并且路径没有处理时,如何避免两台路由器发出<page-not-found>

回答

0

如果其他设置正确,我可以确认<div ng-view></div>在角度2 AppComponent组件中工作。 (添加AppComponentAppModulebootstrap阵列)。

使用HybridUrlHandlingStrategy可防止Angular在请求ng1路由时抛出错误。

添加虚拟路由与空(“”)的模板,以防止NG1从呈现404页时,被请求的页面NG2:

$routeProvider 
    .when('/some/ng1/path', {template: '<something></something>'}) 
    .when('/some/ng2/path', {template: ''}) // ng2 route 
    .otherwise({template: '<page-not-found></page-not-found>'});