带有可选参数并且没有尾部斜杠的应用程序状态

2024-01-08

.state('tabs.map', {
    url:'/map/{location_id}',
    params: {
      location_id: { value: -1 }
    },
    views: {
      'map-tab':{
        templateUrl:'templates/map.html',
        controller:'MapCtrl'
      }
    }
  })

我已经尝试了在网上找到的许多不同的可选参数选项,但没有一个完全像我正在寻找的那样工作。我添加的代码允许:

  • /选项卡/地图/.*?

  • /选项卡/地图/

but not

  • /tab/map

我不确定为什么尾部斜杠会引起问题,因为据我读到的 https://github.com/angular-ui/ui-router/issues/108#issuecomment-40558474这应该不是问题。有谁知道如何解决这个问题?

最近咨询过

  • Angular UI-Router:多个 URL 到单一状态 https://stackoverflow.com/questions/26416554/angular-ui-router-multiple-urls-to-single-state
  • AngularJs UI 路由器 - 具有多个 URL 的一种状态 https://stackoverflow.com/questions/28138169/angularjs-ui-router-one-state-with-multiple-urls
  • angularjs 路由可以有可选的参数值吗? https://stackoverflow.com/questions/17510962/can-angularjs-routes-have-optional-parameter-values
  • Laravel 4 具有多个可选获取参数的路由问题 https://stackoverflow.com/questions/22140898/laravel-4-route-issues-with-multiple-and-optional-get-params

Solution

介绍squash到参数变量

.state('tabs.map', {
    url:'/map/:location_id',
    params: {
      location_id: { value:null, squash:true }
    },
    views: {
      'map-tab':{
        templateUrl:'templates/map.html',
        controller:'MapCtrl'
      }
    }
  })

您可以使用squash参数来允许不带尾部斜杠

  params: {
      location_id: {
        value: null,
        squash: true
      }
   }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有可选参数并且没有尾部斜杠的应用程序状态 的相关文章

随机推荐