2016-04-22 63 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"; 
相關問題