角度:某些路由使用PathLocationStrategy,但其他一些路由则使用HashLocationStrategy [英] Angular: Some routes use PathLocationStrategy but some other routes use HashLocationStrategy

查看:578
本文介绍了角度:某些路由使用PathLocationStrategy,但其他一些路由则使用HashLocationStrategy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app.routes中有4条路由.

I have 4 routes in my app.routes.

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

export const pageRoutes: Routes = [
  {path: '', redirectTo: 'home', pathMatch: 'full'},
  {path: 'home', component: HomeComponent},
  {path: 'transaction', loadChildren: './app/transaction.module#TransactionModule'},
  {path: 'evidence', loadChildren: './app/evidence.module#EvidenceModule'}
];

@NgModule({
  imports: [RouterModule.forRoot(pageRoutes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}

在app.routes中,我没有为LocationStrategy做任何特定的设置.由于对于transaction路由,我想使用angular2默认的PathLocationStrategy,它不允许用户刷新页面.

In the app.routes, I didn't do any specific setup for LocationStrategy. Since for the transaction route, I want to use the angular2 default PathLocationStrategy, which doesn't allow the user to refresh the page.

但是对于evidence路由,我实际上希望用户能够刷新页面.所以我想在这里使用HashLocationStrategy.

But for the evidence route, I actually want the users to be able to refresh the page. So I'd like to use HashLocationStrategy here.

这是evidence-routing.module

    @NgModule({

          imports: [RouterModule.forChild([
            {path:':sessionId', component: EvidenceComponent}
            { path: '**', redirectTo: '/404' },
            { path: '404', component: PageNotFoundComponent}
          ])], 
          exports: [RouterModule],
          providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]

    })
    export class EvidenceRoutingModule {}

我想在evidence-routing.module内添加providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }]以启用HashLocationStrategy,并且仅适用于此路由.

I wanted to add providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }] inside evidence-routing.module to enable HashLocationStrategy and only apply to this route.

但是一旦我把它放在那里,整个应用程序将采用HashLocationStrategy,它也适用于事务处理路由.

But once I put it there, the entire application will adopt the HashLocationStrategy, it also works for the transaction route.

我找不到任何好的解决方案来解决这个问题.

I couldn't find any good solutions to handle this.

关于这个问题有什么建议吗?

Any advice on this issue?

非常感谢!

推荐答案

您不能对不同的路由使用不同的LocationStrategy,此设置用于Router,而不是路由,并且只能用于一台路由器用于一个应用程序.

You can't use a different LocationStrategy for different routes, this setting is for the Router, not for routes, and there can only be exactly one router for one application.

您应该确保服务器配置正确,然后即使使用PathLocationStrategy,重载也将适用于所有路由.

You should rather ensure the server is configured properly, then reload will work for all routes even with PathLocationStrategy.

确保您的服务器配置为支持HTML5 pushState.
这只是意味着服务器将index.html的请求返回给未知资源.

Ensure your server is configured to support HTML5 pushState.
This just means that the server returns index.html for requests to unknown resources.

这篇关于角度:某些路由使用PathLocationStrategy,但其他一些路由则使用HashLocationStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆