简体   繁体   English

使用ES6和Webpack导入角度服务

[英]importing angular services with ES6 and webpack

I'm trying to import $timeout with ES6 and webpack and I keep getting that $timeout is undefined. 我正在尝试使用ES6和webpack导入$ timeout,但我一直在获取$ timeout是未定义的。 Can anyone help? 有人可以帮忙吗? If there's a way that doesn't involve using $inject I'd prefer it because I'm gradually trying to get rid of angularjs in my code. 如果有一种方法不涉及使用$ inject,我会更喜欢它,因为我正在逐渐尝试摆脱代码中的angularjs。

randomTVNames.service.js: randomTVNames.service.js:

import angular from 'angular';

class RandomTVNames {
    constructor($timeout) {
        this.tv = ['Shield', 'Walking Dead', 'Castle', 'Leftovers'];
        this.timeout = $timeout;
        console.log(this.timeout);
    }

    getName() {
        const totalNames = this.tv.length;
        const rand = Math.floor(Math.random() * totalNames);
        return this.tv[rand];
    }

    getTimeout(){

        this.timeout(function () {
            alert("this is timeout")}, 3000);
    }
}

RandomTVNames.$inject = ['$timeout'];

//todo - try to inject angular argument (such as $timeout) with $inject
var randomTVNames = new RandomTVNames();

export default randomTVNames;

home.controller.js: home.controller.js:

import randomTVNames from '../../services/randomTVNames.service';
import mainModule from '../../mainModule';

class HomeController {
    constructor() {
        this.tv = randomTVNames;
        this.name = 'World';
    }

    randomTVName($timeout) {
        this.name = this.tv.getName();
    }

    getCtrlTimeout(){
        this.tv.getTimeout();
    }

}

mainModule.controller('HomeController', HomeController);

ES6 modules are not compatible with the module system from Angular 1.x. ES6模块与Angular 1.x的模块系统不兼容。 This means that export ing and import ing services and controllers won't work, you need to register and inject them using Angular's module system. 这意味着exportimport服务及控制器将无法正常工作,您需要使用Angular的模块系统进行注册和注入。

randomTVNames.service.js: randomTVNames.service.js:

import mainModule from '../../mainModule';

class RandomTVNames {
    // ... snip ...
}

RandomTVNames.$inject = [ /* ... snip ... */ ];

mainModule.service('randomTVNames', RandomTVNames);

home.controller.js: home.controller.js:

import mainModule from '../../mainModule';

class HomeController {
    constructor($scope, randomTVNames) {
        this.$scope = $scope;
        this.tv = randomTVNames;
    }
}

HomeController.$inject = ['$scope', 'randomTVNames'];

mainModule.controller('HomeController', HomeController);

Then in your main webpack file, make sure to import both of them so they get bundled: 然后在您的主要webpack文件中,确保将它们都导入,以便将它们捆绑在一起:

import 'services/randomTVNames.service';
import 'controllers/controller.service';

There is no way to get rid of $inject, unless you are not minifying you code (but i hope you are). 没有摆脱$ inject的方法,除非您没有缩小代码(但我希望您是)。

Angular is injecting variables using their names, so when it sees $scope, it know to look for it and inject it, but when minifying your code the variable names are changed ($scope becomes c etc.) and angular does not know what object you want to inject. Angular正在使用变量的名称注入变量,因此,当它看到$ scope时,便知道要查找并注入它,但是在最小化代码时,变量名称便被更改($ scope变为c等),而angular不知道哪个对象你想注入。

That is what $inject is for since strings are not minified. 那是$ inject的目的,因为没有缩小字符串。

please take a look at this loader . 请看看这个装载机 it helps with what you're trying to do. 它有助于您尝试做的事情。 but mind adding 'ngInject' anywhere in your js file, that injects anything 但是请注意在您的js文件中的任意位置添加“ ngInject”,这会注入任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM