2017-06-08 24 views
0

我是Angular 4的初學者,我在ng-2 ui-router-2中使用尾部斜線問題。使用ng-2 ui-router-2處理斜角4中的尾部斜線

當我打這個URL,它工作正常

http://localhost:8000/dashboard/home

,但通過添加/在年底,它不會顯示頁面

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

我搜查了它,b ut找到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路由器-2這樣做是角4?

回答

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 {} 
+0

克里斯可以請你告訴我在哪裏把這個代碼?在State.ts文件或模塊ts中。因爲我是初學者,所以我不能把它放在哪裏 –

+0

@MuhammadHassanRiazYousufi我更新了答案 –