我正在尝试在Ionic2/Angular2中开发一个应用程序 我想声明一个简单的模型并得到编译错误 如下。请帮助指出编译错误的原因。
模型1:global.data.ts
import {Injectable} from "angular2/core";
@Injectable
export class GDService{
private _newpost:string;
private _allpost:string;
}
Compilation error:
import {Injectable} from "angular2/core";
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
SyntaxError: global.data.js: Unexpected token (5:9) while parsing file:
模型2:job.mdl.ts
export class Job{
constructor(
id:number,
category:string,
type:string,
details:string
){
}
}
Compilation error:
export class Job{
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
编辑:
它可能与您的 tsconfig 文件相关,请确保您在那里定义了一个模块,例如:
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
请尝试这个兄弟。
而不是这一行
import {Injectable} from "angular2/core";
尝试像这样给它
import {Injectable} from "@angular/core";
(请在您的 package.json 文件中检查一次是否存在@angular/核心依赖项。
希望这对你有帮助。有好的一天。