【问题标题】:Handle Trailing Slash in angular 4 using ng-2 ui-router-2使用 ng-2 ui-router-2 处理 Angular 4 中的尾部斜线
【发布时间】:2017-11-10 05:10:09
【问题描述】:

我是 Angular 4 的初学者,我在 ng-2 ui-router-2 中遇到斜杠问题。

当我点击这个网址时,它工作正常

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 中做到这一点?

【问题讨论】:

    标签: javascript angular angular-ui-router


    【解决方案1】:

    这是使用UrlService 配置的。将一些代码添加到设置 strictMode 的路由器配置函数中。

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

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

    【讨论】:

    • 克里斯你能告诉我把这段代码放在哪里吗?在 State.ts 文件或模块 ts 中。因为我是初学者,所以我无法将它放在哪里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2018-05-01
    • 2014-11-18
    • 2015-10-16
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多