ngAnnotate不适用于没有实例方法的ES6类

时间:2015-11-29 19:06:22

标签: angularjs ecmascript-6 constructor-injection

我已经通过关于不正确的依赖注入以及由此导致的缩小代码错误的帖子,并且我们无法弄清楚为什么我会在这里得到它。

缩小时会产生$injector:unpr错误

// Projects.projects = ['array', 'of', 'stuff']
import { Projects } from './projects';

export class PortfolioController
{
    /* @ngInject */
    constructor($state)
    {
        this.$state = $state;
        this.projects = new Projects().projects;
    }
}

这会毫无错误地缩小

import { Projects } from './projects';

export class PortfolioController
{
    /* @ngInject */
    constructor($state)
    {
        this.$state = $state;
        this.projects = new Projects().projects;
        this.Init();
    }

    Init()
    {
        this.$state.name === 'portfolio' ? 
            console.log('PortfolioController instantiated') : 
            console.log('no-go');
    }
}

控制器注册/状态配置,以防

'use strict';

import { PortfolioController } from './portfolio.controller';

angular.module('myApp')
    .controller('PortfolioController', PortfolioController)
    .config(['$stateProvider', Config]);


function Config($stateProvider)
{
    // NOTE: adding this fixes the issue, but this does not seem like an ideal scenario
    PortfolioController.$inject = ['$state'];

    $stateProvider

    .state('portfolio',
    {
        url: '/portfolio',
        views: {
            'portfolio': {
                templateUrl: 'app/portfolio/views/portfolio.html',
                controller: PortfolioController,
                controllerAs: 'vm'
            }
        }
    });
}

想法?

更新 - 认为包含模块setter可能会有所帮助。

'use strict';

angular.module('myApp', ['ngCookies', 'ngResource', 'ngSanitize', 'ui.router', 'ui.bootstrap', 'ngAnimate'])
    .config(['$urlRouterProvider', '$locationProvider', '$animateProvider', Config])
    .run(['$rootScope', '$state', Run]);

function Config($urlRouterProvider, $locationProvider, $animateProvider)
{
    // basic ui.router config
}

function Run($rootScope, $state)
{
    // actions that occur when the app runs
}

1 个答案:

答案 0 :(得分:0)

我认为它与注释无关,因为即使在代码的缩小版本中它们也应该正常工作。

我可以肯定地告诉您,这也可能会给您提供确切的错误消息,即您使用ui.router,但您不会注入您的模块中的依赖关系。

确保在浏览器中加载了库,然后将其注入模块:

angular.module('myApp', ['ui.router'])
    .controller('PortfolioController', PortfolioController)
    .config(['$stateProvider', Config])