使用NG-2 UI-ROUTER-2在Angular 4中处理后斜线



我是Angular 4的初学者,我在NG-2 UI-Router-2中遇到斜线问题。

当我击中此URL时,它的工作正常

http://localhost:8000/dashboard/home

但是,通过添加/结束,它不会显示页面

http://localhost:8000/dashboard/home/

我搜索了它,但是找到了UI-Router 1的解决方案,其中之一是使用

$ urlmatcherfactoryprovider.strictmode(false(;

有人说要添加这样的规则

  $urlRouterProvider.rule(function($injector, $location) {
var path = $location.path();
var hasTrailingSlash = path[path.length-1] === '/';
if(hasTrailingSlash) {
  //if last charcter is a slash, return the same url without the slash  
  var newPath = path.substr(0, path.length - 1); 
  return newPath; 
}});

使用NG2 UI-ROUTER-2 ??

在Angular 4中如何执行此操作

使用UrlService配置。将一些代码添加到您的路由器配置函数,该函数设置了严格模式。

https://ui-router.github.io/ng2/docs/latest/interfaces/url.url.urlconfigapi.html#strictmode

export function routerConfig(router: UIRouter) {
  router.urlService.config.strictMode(false);
}
@NgModule({
  imports: [        
    UIRouterModule.forRoot({ config: routerConfig })
  ]
}) export class AppModule {}

最新更新