自定义路由提供程序不是功能时 [英] Custom Route Provider when is not a function
问题描述
这是从今天早上开始的问题的更新版本.
This is an updated version of a question from this morning.
我有两个独立的模块,管理员"和授权".在授权的提供程序块中,我扩展了 $ routeProvider
以便将相同的路由附加到所有路由定义.
I have two separate modules, 'admin' and 'authorization'. In the provider block of authorization, I extend $routeProvider
in order to attach an identical route to all of the route definitions.
首先,这是admin.js:
First, here is admin.js:
angular.module('authorization', [])
.provider('$appRoute', function () {
this.$get = function($routeProvider) {
var universalResolves = {authorize: function($authorization) {
return $authorization.authorize();
}};
var extendedRouter = angular.extend({}, $routeProvider, {
when: function(path, route) {
route.resolve = (route.resolve) ? route.resolve : {};
angular.extend(route.resolve, universalResolves);
$routeProvider.when(path, route);
return this;
}
});
return new extendedRouter();
}
})
.factory('$authorization', ['$http', '$location', function($http, $location) {
var $authorization = {};
$authorization.authorize = function() {
var path = $location.path();
return promise = $http.get('/svc/authorize/view?urlPath=' + path).then(function(response) {
var data = response.data;
if (response.data.result === 'NOT_AUTHORIZED') {
throw "NOT_AUTHORIZED";
}
return data;
});
};
return $authorization;
}]);
接下来,我的管理模块:
Next, my admin module:
angular.module('admin', ['ngRoute', 'ngSanitize', 'ngCsv'
, 'authorization'
])
.controller('mainCtrl', function() {
})
.config(['$routeProvider', '$appRouteProvider', function($routeProvider, $appRouteProvider) {
// old definition that needs to be changed to $appRouteProvider
$routeProvider.when('/login', {
templateUrl: '/login/auth.html',
controller: 'loginCtrl'
});
$appRouteProvider.when('/page', {
templateUrl: 'page.tmpl.html',
controller: 'pageCtrl'
});
不幸的是,我收到以下错误:
Unfortunately, I get the following error:
Error: [$injector:modulerr] Failed to instantiate module app due to:
$appRouteProvider.when is not a function
@https://localhost:8443/admin.js:87:9
invoke@https://localhost:8443/js/angular/angular.js:4718:16
runInvokeQueue@https://localhost:8443/js/angular/angular.js:4611:11
loadModules/<@https://localhost:8443/js/angular/angular.js:4620:11
forEach@https://localhost:8443/js/angular/angular.js:321:11
loadModules@https://localhost:8443/js/angular/angular.js:4601:5
loadModules/<@https://localhost:8443/js/angular/angular.js:4618:40
forEach@https://localhost:8443/js/angular/angular.js:321:11
loadModules@https://localhost:8443/js/angular/angular.js:4601:5
createInjector@https://localhost:8443/js/angular/angular.js:4523:19
doBootstrap@https://localhost:8443/js/angular/angular.js:1758:20
bootstrap@https://localhost:8443/js/angular/angular.js:1779:12
angularInit@https://localhost:8443/js/angular/angular.js:1664:5
@https://localhost:8443/js/angular/angular.js:31763:5
j@https://localhost:8443/js/jquery/jquery.min.js:2:29566
g/</k<@https://localhost:8443/js/jquery/jquery.min.js:2:29882
因此它看到了$ appRouteProvider,但只看到了 this.$ get()
方法.有人可以帮忙吗?
So it sees the $appRouteProvider, but only the this.$get()
method. Can anyone help?
推荐答案
仅就该错误而言, $ appRoute
提供程序不会在其自身上公开任何方法(其他方法除外 $ get
).因此,它是一个空的api配置提供程序对象.
Just regarding the error, the $appRoute
provider is not exposing any methods on it self (other then $get
). So its an empty api config provider object.
在使用基本提供者配方时, $ get
方法用于提供生成服务的功能,以及提供者提供的
用于绑定额外的配置功能,以放置 this
$ get
函数以后可以使用的数据.
When using the base provider recipe, the $get
method is used to provide a function to generate the service, and the this
from the provider
is used to bind extra config functions to put data the $get
function can later use.
来自角度提供者文档:
myApp.provider('unicornLauncher', function UnicornLauncherProvider() {
var useTinfoilShielding = false;
// provider config api method.
this.useTinfoilShielding = function(value) {
useTinfoilShielding = !!value;
};
this.$get = ["apiToken", function unicornLauncherFactory(apiToken) {
// let's assume that the UnicornLauncher constructor was also changed to
// accept and use the useTinfoilShielding argument
return new UnicornLauncher(apiToken, useTinfoilShielding);
}];
});
如果要在提供程序上添加其他方法,则可以使用 this.when = ...
或 Angular.extends(this,....)
例如.
If you want to add additional methods on the provider, you can use this.when = ...
or Angular.extends(this, ....)
for instance.
这篇关于自定义路由提供程序不是功能时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!