2016-04-22 41 views
2

我将角度应用转换为使用ES6样式语法,并且在更改所有文件后出现以下错误;angular.js:68未捕获的错误:[ng:areq]参数'fn'不是函数,得到对象

angular.js:68未捕获的错误:[NG:AREQ]参数“FN”不是一个函数,得到了对象

当我试着调试错误似乎在创建服务中来的错误。在角码这是在错误发生的事情

return cache[serviceName] = factory(serviceName, caller); 

我看到获得通过精细的服务名,调用者怎么过是不确定的,不知道为什么工厂是无法返回的服务实例。

感谢

+0

“调用者”在哪里定义? – azium

+0

这就是角码而不是我们的应用程序代码。 –

+0

角。js行4571方法名称getService(serviceName,caller))我正在使用的角度版本是1.5.3 –

回答

0

这里是我的github帐户的完整代码。 https://github.com/mars76/angular-es6

我的切入点是app.js

import * as angular from 'angular'; 

import config from './app-config'; 

import run from './app-run-config'; 

import uiRouter from 'angular-ui-router'; 

import AppController from './app-controller'; 

import MyService from './my-service'; 

import * as serviceModule from './service'; 

import testDirective from './test-directive'; 

import muUtil from './util/util.js'; 
    export default angular.module("my-app", ['ui.router','my-service-module','myUtil']) 

     .config(config) 

     .run(run) 

     .component('testDirective',testDirective()) 
     .controller('appController', AppController).name; 

这对模块的依赖关系 “myUtil”

,这里是该模块

./util/的内容util.js

import * as utilRun from './util-run.js'; 
import * as MyService1 from './my-service1.js'; 
import * as MyService2 from './my-service2.js'; 

    export default angular.module('myUtil', []) 

     .service('MyService1', MyService1) 

     .service('MyService2', MyService2) 

     .run(utilRun).name; 

这里是我的Se rvices

服务1:

export default class MyService1{ 
    constructor(){ 

    } 

    init(){ 
     console.log('In MyService1()'); 
    } 
} 

服务2:

export default class MyService2{ 
    constructor(){ 

    } 

    init(){ 
     console.log('In MyService2()'); 
    } 
} 

而且UTIL模块

import MyService1 from './my-service1.js'; 
import MyService2 from './my-service2.js'; 

export default function utilRun(MyService1,MyService2){ 

    MyService1.init(); 
    MyService2.init(); 

} 

utilRun.$inject =['MyService1','MyService2']; 
0

的运行功能我有同样的问题。在我的情况下,这只是进口问题。

本文档帮助我:import - JavaScript | MDN

的服务导出为default,应该在你的./util/util.js通过导入为default

import MyService1 from './my-service1.js'; 
import MyService2 from './my-service2.js'; 

我建议的错误

angular.js:68 Uncaught Error: [ng:areq] Argument 'fn' is not a function, got Object 

有原因是,以这种方式导入的服务为undefined

import * as MyService1 from './my-service1.js'; 
import * as MyService2 from './my-service2.js'; 

通配符导入不导入默认值。这就是为什么在ES6语法中必须有特殊情况的原因:

import defaultMember, * as name from "module-name"; 
相关问题