问题描述
我写了一个 angularjs
工厂如下
I have written an angularjs
factory as below
module.factory('LogService', function () {
function log(msg) {
console.log("Rahkaran:" + new Date() + "::" + msg);
}
return
{
log: log
};
});
但是我一直收到这个错误
But I kept getting this error
Provider 'LogService' 必须从 $get 工厂方法返回一个值
我在谷歌上搜索了错误,但找不到任何解决方案.
I googled about the error and I couldn't find any solution.
巧合的是我把return
语句改成了这个
Coincidentally I changed the return
statement to this
return{
log: log
};
错误消失了!!
在 return
前面和在下一行有 {
有什么区别吗?
Is there any differences between having {
in front of return
or at the next line?
推荐答案
这叫做自动分号插入
return 语句受自动分号插入 (ASI) 影响.返回关键字和允许的表达式之间没有行终止符 ;
.
The return statement is affected by automatic semicolon insertion (ASI). There is no line terminator ;
between the return keyword and the expression allowed.
return
a + b;
// is transformed by ASI into
return;
a + b;
所以你必须在 return 前面插入 {
而不要在下一行.
So you must insert {
in front of return and Not at the next line.
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
这篇关于提供者 'xx' 必须从 AngularJs 中的 $get 工厂方法返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!