我正在尝试使用使用ui路由的ncy-angular-breadcrumb。我设置了一个模块来容纳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路由器以及它的工作方式或应做的事情。我只想获取部分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`