AngularJS:url中的可选语言参数

时间:2022-08-24 14:47:56

I'm working with angularjs and UI-Router. I'd like to configure routes which specify the selected language. though this part of the route should be optional.

我正在使用angularjs和UI-Router。我想配置指定所选语言的路由。虽然这部分路线应该是可选的。

I have following states:

我有以下状态:

{
    state: 'app',
    config: {
          abstract: true,
          url: '/{lang:(?:de|en)}',
          template: '<ui-view/>'
    }
}

{
    state: 'app.mainview',
    config: {
          url: '/mainview',
          templateUrl: 'app/mainview/mainview.html',
          controller: 'MainviewController',
          controllerAs: 'vm',
          title: 'Main View',
          settings: {
              pos: 1,
              displayName: 'Mainview',
              icon: 'code-array'
          }
    }
}

now its only possible to navigate to mainview with

现在它唯一可以导航到主视图

example.com/en/mainview

Though I'd like to configure the ui-router so that all o the following routes are valid:

虽然我想配置ui-router以使以下所有路由都有效:

example.com/mainview
example.com/en/mainview
example.com/de/mainview

Can I set a route with an optional language param at the beginning without using a double slash? If no, what alternatives do you suggest?

我可以在开头设置一个带有可选语言参数的路由而不使用双斜杠吗?如果不是,您建议使用哪些替代方案?

1 个解决方案

#1


5  

There is a solution in detail described here

这里有一个详细的解决方案

where you basically introduce parent state

你基本上介绍父州的地方

.state('root', {
    url: '/{lang:(?:en|de|cs)}',
    abstract: true,
    template: '<div ui-view=""></div>',
    params: {lang : { squash : true, value: 'en' }}
})

#1


5  

There is a solution in detail described here

这里有一个详细的解决方案

where you basically introduce parent state

你基本上介绍父州的地方

.state('root', {
    url: '/{lang:(?:en|de|cs)}',
    abstract: true,
    template: '<div ui-view=""></div>',
    params: {lang : { squash : true, value: 'en' }}
})