在ui-router中设置抽象嵌套状态的默认子级

时间:2021-08-10 10:44:14

I use ui-router.
Here are my nested states:

我用的是ui-router。这是我的嵌套状态:

$stateProvider
.state('books', {
  abstract: true,
  url: '/books',
  controller: 'BooksCtrl',
  templateUrl: 'contents/books.html'
})
.state('books.top', {
  url: '/top',
  templateUrl: 'contents/books-top.html'
})
.state('books.new', {
  url: '/new',
  templateUrl: 'contents/books-new.html'
});

How can I set books.new state to be default child of the books abstract state, so then when you hit /books ui-router redirects to /books/new?

如何设置books.new状态为书籍抽象状态的默认子项,那么当你点击/ books ui-router重定向到/ books / new?

2 个解决方案

#1


5  

There is a working example

有一个工作的例子

We can use built in features. 1) default is such child state which has empty url:

我们可以使用内置功能。 1)默认是具有空url的子状态:

$stateProvider
    .state('books', {
      abstract: true,
      url: '/books/new',
      controller: 'BooksCtrl',
      ..
    })
    .state('books.new', {
      //url: '/new',
      url: '',
      ...
    })
    .state('books.top', {
      url: '^/books/top',
      ...
    });

And 2) to keep /books in place, we can use redirection

2)保持/书籍到位,我们可以使用重定向

  $urlRouterProvider.when('/books', '/books/new');

And these links will work as expected:

这些链接将按预期工作:

// href
<a href="#/books">
<a href="#/books/new">
<a href="#/books/top">
//ui-sref
<a ui-sref="books.top">
<a ui-sref="books.new">

Check it here

在这里查看

#2


1  

Try this way:

试试这种方式:

$stateProvider
.state('books', {
  abstract: true,
  url: '/books',
  controller: 'BooksCtrl',
  templateUrl: 'contents/books.html'
})
.state('books.top', {
  url: '/top',
  templateUrl: 'contents/books-top.html'
})
.state('books.new', {
  url: '',
  templateUrl: 'contents/books-new.html'
});

EDIT: I know that's not very nice, but you can create additional state with same arguments except url:

编辑:我知道这不是很好,但你可以使用相同的参数创建其他状态,除了url:

var booksArgs = {
  url: '',
  templateUrl: 'contents/books-new.html'
};
$stateProvider.state('books.new', booksArgs);
$stateProvider.state('books.new_', angular.extend({}, booksArgs, {
    url: '/new'
}));

Another solution from this post:

这篇文章的另一个解决方案

In states configuration:

在状态配置:

$stateProvider
.state('books', {
  url: '/books',
  controller: 'BooksCtrl',
  templateUrl: 'contents/books.html',
  redirectTo: '.new'
})
.state('books.top', {
  url: '/top',
  templateUrl: 'contents/books-top.html'
})
.state('books.new', {
  url: '/new',
  templateUrl: 'contents/books-new.html'
});

On module run:

在模块运行:

app.run(['$rootScope', '$state', function($rootScope, $state) {
    $rootScope.$on('$stateChangeStart', function(evt, to, params) {
      if (to.redirectTo) {
        evt.preventDefault();
        $state.go(to.redirectTo, params, { relative: to });
      }
    });
}]);

#1


5  

There is a working example

有一个工作的例子

We can use built in features. 1) default is such child state which has empty url:

我们可以使用内置功能。 1)默认是具有空url的子状态:

$stateProvider
    .state('books', {
      abstract: true,
      url: '/books/new',
      controller: 'BooksCtrl',
      ..
    })
    .state('books.new', {
      //url: '/new',
      url: '',
      ...
    })
    .state('books.top', {
      url: '^/books/top',
      ...
    });

And 2) to keep /books in place, we can use redirection

2)保持/书籍到位,我们可以使用重定向

  $urlRouterProvider.when('/books', '/books/new');

And these links will work as expected:

这些链接将按预期工作:

// href
<a href="#/books">
<a href="#/books/new">
<a href="#/books/top">
//ui-sref
<a ui-sref="books.top">
<a ui-sref="books.new">

Check it here

在这里查看

#2


1  

Try this way:

试试这种方式:

$stateProvider
.state('books', {
  abstract: true,
  url: '/books',
  controller: 'BooksCtrl',
  templateUrl: 'contents/books.html'
})
.state('books.top', {
  url: '/top',
  templateUrl: 'contents/books-top.html'
})
.state('books.new', {
  url: '',
  templateUrl: 'contents/books-new.html'
});

EDIT: I know that's not very nice, but you can create additional state with same arguments except url:

编辑:我知道这不是很好,但你可以使用相同的参数创建其他状态,除了url:

var booksArgs = {
  url: '',
  templateUrl: 'contents/books-new.html'
};
$stateProvider.state('books.new', booksArgs);
$stateProvider.state('books.new_', angular.extend({}, booksArgs, {
    url: '/new'
}));

Another solution from this post:

这篇文章的另一个解决方案

In states configuration:

在状态配置:

$stateProvider
.state('books', {
  url: '/books',
  controller: 'BooksCtrl',
  templateUrl: 'contents/books.html',
  redirectTo: '.new'
})
.state('books.top', {
  url: '/top',
  templateUrl: 'contents/books-top.html'
})
.state('books.new', {
  url: '/new',
  templateUrl: 'contents/books-new.html'
});

On module run:

在模块运行:

app.run(['$rootScope', '$state', function($rootScope, $state) {
    $rootScope.$on('$stateChangeStart', function(evt, to, params) {
      if (to.redirectTo) {
        evt.preventDefault();
        $state.go(to.redirectTo, params, { relative: to });
      }
    });
}]);