角混合应用程序 - 角2路由器无法正常工作

时间:2018-06-06 13:01:06

标签: angularjs angular angular2-routing aot

我正在尝试运行带有角度2组件的混合应用程序。以下是我为运行混合应用程序所做的设置,我能够运行应用程序,并且“my-app”组件也在此混合应用程序中初始化,但是当我尝试使用路由访问组件(FirstComponent)时(“ / first“),它加载FirstComponent但自动重定向到我的默认角度1路线。

index.html

<base href="/hybridApp/">
<my-app></my-app>

app.component.ts

@Component({
  selector: 'my-app',
  template: '<router-outlet></router-outlet>' +
  '<div ng-view></div>',
})
export class AppComponent  {
  name = 'Angular';
}

app.module.ts

@NgModule({
    imports: [
        BrowserModule,
        UpgradeModule,
        RouterModule,
        FirstModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [ AppComponent ]
})

export class AppModule {
    constructor(private upgrade: UpgradeModule) { }
    ngDoBootstrap() {
    }
}

first.module.ts

export class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy {
    shouldProcessUrl(url: any) {
        return url.toString().startsWith("/first");
    }

    extract(url: any) {
        return url;
    }

    merge(url: any, whole: any) {
        return url;
    }
}

const routes : Routes = [

    {
        path :'first',

        component: FirstComponent
    }
]



@NgModule({
    imports:[
        RouterModule.forRoot(routes,{ useHash: true, initialNavigation: true, enableTracing: true })
    ],
    exports:[

    ],
    declarations:[
        FirstComponent
    ],
    providers:[
        { provide: UrlHandlingStrategy, useClass: Ng1Ng2UrlHandlingStrategy }
    ]
})
export class FirstModule {}

first.component.ts

@Component({
    selector:'first-component',
    templateUrl:'src/app/first.component.html',
    styleUrls:['src/app/first.component.css']
})

export class FirstComponent implements OnInit{
    ngOnInit(){
        console.log("first component rendered..")
    }

    constructor(
        private route:ActivatedRoute
    )
    {}
}

main.ts

platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
    const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
    upgrade.bootstrap(document.body, ['angular1App']);
});

我能够看到来自我的FirstComponent“渲染的第一个组件...”的日志,但不是停留在该路线上,而是重定向到angular1 app的默认路由。

以下是@jeffrey

建议的方法
NavigationStart {id: 8, url: "/first"}
   NavigationStart {id: 8, url: "/first"}
   RoutesRecognized {id: 8, url: "/first", urlAfterRedirects: "/first", state: RouterStateSnapshot}
   RoutesRecognized {id: 8, url: "/first", urlAfterRedirects: "/first", state: RouterStateSnapshot}
   GuardsCheckStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   GuardsCheckStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   GuardsCheckEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
   GuardsCheckEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
   ResolveStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   ResolveStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   ResolveEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   ResolveEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
   NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
   NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
   NavigationStart {id: 9, url: "/auth"}
   NavigationStart {id: 9, url: "/auth"}
   NavigationStart {id: 9, url: "/auth"}
   GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
   GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
   GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
   GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
   NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}
   NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}
   NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}

2 个答案:

答案 0 :(得分:0)

我尝试使用我的ui-router形式angularjs与angular 5应用程序。现在我遇到了http_interceptor的问题,无论如何,首先我必须降级app-component,然后插入我的index.js文件中。第二,我试着知道网址处理策略是否正常,角度5开始管理路由。第三,我必须在angularjs模块中注入angular5 urlHandling策略和angular5的路由。在那之后我的角度5工作了。经过几个小时的搜索,我找到了一个关于这个问题的要点。

链接:

Gist

答案 1 :(得分:0)

要查看路由器的功能,重定向之前,之中和之后,请将其添加到FirstComponent:

constructor(
  private router: Router
) {
  this.router.events.subscribe(e => {
    console.log(e)
  });
}

您必须深入了解事件日志,但它可能会为您提供有价值的见解。

如果仅在部署时遇到此问题,请尝试以下操作:

如果您使用的是apache服务器,请将其添加到.htaccess文件中:

RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    # If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

如果您正在使用Angular团队建议的其他服务器,例如NGinx,IIS或Firebase,take a look here for more configurations