gpt4 book ai didi

javascript - 不是函数时的自定义路由提供程序

转载 作者:行者123 更新时间:2023-11-30 11:04:10 24 4
gpt4 key购买 nike

这是今天早上问题的更新版本。

我有两个独立的模块,“管理”和“授权”。在提供程序授权 block 中,我扩展了 $routeProvider 以便将相同的路由附加到所有路由定义。

首先,这是 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;
}]);

接下来,我的管理模块:

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'
});

不幸的是,我收到以下错误:

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() 方法。谁能帮忙?

最佳答案

就错误而言,$appRoute 提供程序没有在其自身上公开任何方法($get 除外)。所以它是一个空的 api 配置提供程序对象。

当使用基本提供者配方时,$get 方法用于提供生成服务的函数,this 来自provider 用于绑定(bind)额外的配置函数以放置 $get 函数稍后可以使用的数据。

来自 Angular provider docs :

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, ....)例如。

关于javascript - 不是函数时的自定义路由提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com