打字稿:无法在构造函数中使用 : 符号编译文件
Typescript: can't compile file with : notation in constructor
我正在关注Ionic(使用Angular 2)应用程序的一些示例。所有类的构造函数都有类似的东西:
export class UserService {
constructor(http: Http) {
this.http = http;
}
}
但编译器输出此错误:
Unexpected token (14:17)
12 | }
13 |
> 14 | constructor(http: Http)
at Parser.pp.raise (/home/cbenseler/dev/apps/expressoapp/node_modules/babylon/index.js:1425:13)
这可能是我的应用程序(使用 babel 和 webpack)的设置问题,但我找不到问题所在。
webpack.config.js 文件包含以下内容:
module: {
loaders: [
{
test: /'.js$/,
loader: 'babel',
query: {
presets: ['es2015'],
plugins: ['transform-decorators-legacy']
},
include: path.resolve('app'),
exclude: /node_modules/
},
{
test: /'.js$/,
include: path.resolve('node_modules/angular2'),
loader: 'strip-sourcemap'
}
],
noParse: [
/es6-shim/,
/reflect-metadata/,
/zone'.js('/|'')dist('/|'')zone-microtask/
]
}
有人知道吗?
我会看到一件可能会错过的事情:
-
您忘记导入
Http
类,因此编译器无法解析它:import {Http} from 'angular2/http';
编辑
如果仅使用 ES6(似乎是这种情况),则可以在参数级别使用类型。在这种情况下要注入,您需要使用这样的方法:
@Injectable()
export class UserService {
constructor(http) {
this.http = http;
}
static get parameters() {
return [[Http]];
}
}
有关更多详细信息,请参阅以下链接:
- angular2 在使用 ngFormModel (ES6) 时无法读取未定义的属性"验证器"
- https://medium.com/@euphocat/angular2-router-in-es6-7-and-dependency-injection-b96944c3ba2e#.qat3cl6b4
相关文章:
- 在.csv文件中写入学位符号
- 使用相同的数据集绘制各种符号
- AngularJS指令出错-无法读取属性'编译'的未定义
- 将带点符号的属性(有时)传递给函数
- RegEx JavaScript:数字后的符号提取
- 如何使用ViewCompiler手动编译DOM的一部分
- 预编译的车把模板使文件大小加倍
- Mocha react本地路由器编译错误
- 如何在计算器符号为零时替换它
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- 删除输入中输入的符号
- 如何在ReactJs中渲染重音符号
- 如何替换JavaScript字符串中除字母和数字之外的所有内容(空格/符号)
- 在编译阶段后创建新的DOM树,或者继续使用原始修改的DOM
- 如何在Windows中将Javascript文件编译成二进制文件
- 插入符号到底是什么
- 删除图形和数字之间的连字符(-)符号
- 浏览器是否持久缓存脚本元素的编译版本
- GWT:有没有一种方法可以修改GWT在编译中使用的Cast.java文件
- 打字稿:无法在构造函数中使用 : 符号编译文件