我正在尝试使用 ncy-angular-breadcrumb,它使用 ui 路由。我设置了一个模块来容纳 ui 路由:
(function () {
var app = angular.module('configurator.uiroutes', ['ui.router', 'ncy-angular-breadcrumb']);
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('home', {
url: '/',
ncyBreadcrumb: {
label: 'Series'
}
});
}]);
})();
我进行了此测试来查看该路线是否有效:
describe('Configurator UI Routes', function () {
beforeEach(module('configurator.uiroutes'));
beforeEach(module('ui.router'));
var $state, $rootscope, state = '/';
// Inject and assign the $state and $rootscope services.
beforeEach(inject(function (_$state_, _$rootScope_) {
$state = _$state_;
$rootscope = _$rootScope_;
}));
//Test whether the url is correct
it('should activate the state', function () {
$state.go(state);
$rootscope.$digest();
expect($state.current.name).toBe('home');
});
});
这会产生此错误:
Error: Could not resolve '/' from state ''
我只是不知道 UI Router 以及它是如何工作的,或者它应该做什么。我只想获取部分 url 并将该信息传递给 ncyBreadcrumb,但我什至无法让基本 URL 正常工作。
任何指示或帮助都会很棒!
编辑:我通过在测试中用“home”替换“/”来通过测试。我更大的问题是:有没有办法测试当 URL“/”被点击时,state.current.name 变成“home”。在我使用该应用程序时,这种情况似乎没有发生,我希望单元测试可以帮助告诉我原因。
这是一个单页应用程序(如果相关的话)。
最佳答案
错误是足够不言自明的,因为您要求 ui-router
使用 $state.go
重定向到 /
状态
基本上是$state.go
,它要求提供州名称而不是url
。
var $state, $rootscope, state = 'home'; //<--changed from `/` to `home`
关于javascript - 设置和测试 UI 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659141/