Minification issue on route config angular js & typescript min safe

大城市里の小女人 提交于 2020-01-07 03:52:26

问题


I have a bundle with several js files (generated by typescript files)

When I use minification on the bundle, the page fails.

The error on chrome console:

Uncaught Error: [$injector:modulerr] Failed to instantiate module AppModule due to: Error: [$injector:unpr] Unknown provider: n

I isolated the problem to one of two files, the app.module and the app.routes as follows:

app.module:

((): void=> {
    var app = angular.module("AppModule", ['ngRoute', 'ngMessages', 'app.xxx.xxx.xxxModule', 'ngSanitize']);
    app.config(AppModule.Routes.configureRoutes);
})();

app.routes:

/// <reference path="../../../../../scripts/typings/angularjs/angular.d.ts" />
/// <reference path="../../../../../scripts/typings/angularjs/angular-route.d.ts" />
module AppModule {
    declare var staticsDir: string;

    export class Routes {
        static $inject = ["$routeProvider"];
        static configureRoutes($routeProvider: ng.route.IRouteProvider) {
            $routeProvider.when("/xxx", { controller: "Uni.controllers.xxx", templateUrl: staticsDir + "/projects/xxx/xxx/xxx/xxx.html", controllerAs: "xxx" });
            $routeProvider.when("/xxx2", { controller: "Uni.controllers.xxx2", templateUrl: staticsDir + "/projects/xxx2/xxx2/xxx2/xxx2.html", controllerAs: "xxx2" });
        }
    }
}

(ignore the xxx and module names, I sanitized them)

I am guessing the error I am seeing is regarding the $routeProvider. Is there problem in the inject? How should I inject this min safe?


回答1:


You are asking for $routeProvider to be injected in your static function which is not min-safe. static $inject applies to Routes class constructor only. So you can either do:

app.config(['$routeProvider', AppModule.Routes.configureRoutes])

Or if you want to make full use of Angular's DI:

    export class Routes {
        static $inject = ["$routeProvider"];

        constructor(private $routeProvider: ng.route.IRouteProvider){
        }
        configureRoutes() {
            this.$routeProvider.when("/xxx", { controller: "Uni.controllers.xxx", templateUrl: staticsDir + "/projects/xxx/xxx/xxx/xxx.html", controllerAs: "xxx" });
            this.$routeProvider.when("/xxx2", { controller: "Uni.controllers.xxx2", templateUrl: staticsDir + "/projects/xxx2/xxx2/xxx2/xxx2.html", controllerAs: "xxx2" });
        }
    ...
    ...
    }

register it as a provider itself:

ie. app.provider('myRoutes', Routes);

and inject it into the config block:

app.config(['myRoutes', function(myRoutes){
    myRoutes.configureRoutes();
}]

(or sth similar)



来源:https://stackoverflow.com/questions/33011500/minification-issue-on-route-config-angular-js-typescript-min-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!