简体   繁体   English

如何使用UI-Router定义可选参数而不使用尾部斜杠?

[英]How to define optional parameter using UI-Router without trailing slash as well?

I was working on migration of my Angularjs code from ng-router to UI-Router. 我正在努力将我的Angularjs代码从ng-router迁移到UI-Router。 In my ng-router I have optional parameters however I realized that same easy way to support optional parameter is not supported in ui-router. 在我的ng-router中我有可选参数但是我意识到在ui-router中不支持同样简单的方法来支持可选参数。 Following is my routes from my existing ng-router module. 以下是我现有的ng-router模块的路由。

//Define routes for view  using ng-router
$routeProvider.
        when('/my-app/home/:userid?', {
            templateUrl: '/templates/partials/home/HomeView.html',
            controller: 'HomeViewController'
        })                   
        .when('/my-app/:productKey/:userid?', {
            templateUrl: '/templates/partials/productpage/ProductPageView.html',
            controller: 'ProductViewController'
        })
        .otherwise({redirectTo: '/my-app/home'});

Note: If you see ":userid" parameter is very smoothly implemented here as optional. 注意:如果您看到“:userid”参数在这里非常顺利地实现为可选。

I tried following in ui-router however same is not working. 我尝试在ui-router中跟随但是同样不起作用。 I have gone through many post and it was suggested to duplicate the routes. 我经历了很多帖子,建议复制路线。 I really don't want to duplicate as it will make code dirty. 我真的不想复制,因为它会使代码变脏。

//Set the default route

$urlRouterProvider.otherwise('/my-app/home');
//Define routes for view   (Please make sure the most matched pattern is define at top)
$stateProvider
        .state('viewallcards', {
            url: '/my-app/home/:userid',
            templateUrl: '/templates/partials/home/HomeView.html',
            controller: 'HomeViewController'
        })
        .state('productpage', {
            url: '/my-app/:productKey/:userid',
            templateUrl: '/templates/partials/productpage/ProductPageView.html',
            controller: 'ProductViewController'
        });

Here /my-app/home/ works however /my-app/home doesn't work. 这里/ my-app / home /但是/ my-app / home不起作用。 How to make it without trailing slash as well? 怎么做也没有尾随斜线?

Also following post on same issue has surprised to me to know that this is not supported in ui-router. 在同一个问题上关注帖子也让我感到惊讶,知道ui-router不支持这个。 Is it still valid? 它仍然有效吗? Please help. 请帮忙。

There is a working example 一个工作的例子

We can use params : {} object to define the userid exactly as we need (ie ready to be omitted) . 我们可以使用params : {} object来完全按照我们的需要定义userid (即准备省略)

Check more details about params here: 在这里查看有关params更多详情:

Angular ui router passing data between states without URL Angular ui路由器在没有URL的状态之间传递数据

  $stateProvider
    .state('viewallcards', {
      url: '/my-app/home/:userid',
      params: { 
        userid: {squash: true, value: null }, // this will make both links below working:
        //         #/my-app/home
        //         #/my-app/home/
      },
      ...
    })
    .state('productpage', {
      url: '/my-app/:productKey/:userid',
      ...
    })

Check it in action here 在这里检查它

By default, UI Router is in "strict mode" - where it differentiates between routes with a trailing slash and without. 默认情况下,UI路由器处于“严格模式” - 它区分具有尾部斜杠的路径和不具有斜杠的路径。 You can turn this off by disabling this mode: 您可以通过禁用此模式关闭此功能:

How can I have my routes be activated when the url contains either a trailing slash or not? 当网址包含尾部斜杠时,如何激活我的路由?

Set strictMode to false in your config block: 在配置块中将strictMode设置为false:

$urlMatcherFactoryProvider.strictMode(false)

For older version of ui-router, add this snippet to your module's config function. 对于旧版本的ui-router,请将此代码段添加到模块的配置功能中。 It creates a rule on $urlRouterProvider that maps all urls that are missing a trailing slash to the same url but with the trailing slash appended. 它在$ urlRouterProvider上创建一个规则,该规则将所有缺少尾部斜杠的URL映射到同一个URL,但附加了斜杠。

Using this, it's best to define your optional parameters as URL parameters in the style of home?userid=foo , and set stuff that you always expect to see in the URL. 使用此选项,最好将可选参数定义为home?userid=foo样式的URL参数home?userid=foo ,并设置您希望在URL中看到的内容。

From the UI-Router's Wiki . 来自UI-Router的Wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM