【发布时间】:2016-02-28 20:32:46
【问题描述】:
我尝试在 Ionic 2 应用程序的代码中使用 Injectable,但出现此错误。
模块构建失败:SyntaxError: /home.js: Unexpected token (10:25)
export class HomePage {
constructor(myservice: WpService) {
^
this.service = myservice;
this.data = null;
}
这是我的代码:(home.js 文件)。
import {Page} from 'ionic-framework/ionic';
import {WpService} from './wpservice';
@Page({
templateUrl: 'build/pages/home/home.html',
providers: [WpService]
})
export class HomePage {
constructor(myservice: WpService) {
this.service = myservice;
this.data = null;
}
retrieve() {
this.service.loadData();
setTimeout(() => {
this.data = this.service.getData();
console.log(this.data);
}, 5000);
}
}
这是 wpservice 文件:
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/Rx'
@Injectable
export class WpService {
constructor(http: Http) {
this.http = http;
this.data = null;
}
loadData() {
this.http.get('<some rest api>').subscribe(data => {
this.data = data.json()
});
}
getData() {
return this.data;
}
}
奇怪的是,这个错误只发生在 2 月 26 日晚上。在此之前它工作正常。
【问题讨论】:
-
我不能确切地知道是什么导致了错误。但我注意到一些问题: 1- wpservice 文件末尾有一个额外的“{”。 2-你的@Injectable >> 应该是@Injectable()。 3-您的代码是 Typescript,但您的文件是 .js
-
@Abdulrahman:非常感谢先生;从编辑器复制/粘贴代码时 } 是一个错误,Ionic 2 使用带有 ES6 的 js 文件(与打字稿相同),所以我认为这可能不是问题所在。
标签: angular ionic2 injectable