【发布时间】:2017-11-15 21:27:45
【问题描述】:
我一直在尝试重写我们的一些代码以符合 ES6 并且遇到了以下问题。
angular.js:63 Uncaught Error: [$injector:modulerr] 无法实例化模块应用程序,原因是: 错误:[$injector:pget] Provider 'books' 必须定义 $get 工厂方法。
这是我在这段代码中编写提供者对象的时候。
(() => {
const app = angular.module('app', []);
const books = () => { // Error is here
let includeVersionInTitle = false;
this.$get = () => {
const appName = "Book Logger";
const appDesc = "Track which books you read.";
let version = "1.0";
appName = (includeVersionInTitle) ? (appName += " " + version) : appName;
return {
appName: appName,
appDesc: appDesc
}
};
this.setIncludeVersionInTitle = (value) => {
includeVersionInTitle = value;
};
};
app.provider("books", [books]);
})();
当我将const books = () => { ... } 更改为此时,const books = function() { ... }。它会工作并且不会抛出那个错误。
我以为const a = function() { ... } 和const a = () => { ... } 哪里一样?为什么会抛出这个错误?
【问题讨论】:
-
箭头函数不是构造函数。当它被调用时,它永远不会让它的
this成为一个新的“实例”。 -
不,它们不是一回事。查看副本。
标签: javascript angularjs ecmascript-6 function-expression