Ionic 2 - Services unexpected token error

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:47:38

问题


I am trying to use an Injectable in my code in an Ionic 2 app and I get this error.

Module build failed: SyntaxError: /home.js: Unexpected token (10:25)

export class HomePage {
constructor(myservice: WpService) {
                     ^
         this.service = myservice;
        this.data = null;
     }

This is my code: (home.js file).

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);
    }
}

and this is the wpservice file:

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;
    }
    }

Strangely this error is occurring only from feb 26 evening. Before that it was working fine.


回答1:


Thanks guys, I resolved this issue. I'll post how I did it below so that anyone else facing the same would get benefited.

I wrote a get parameters() method as shown below. (After having a look at the ionic conferencing app from the drifty co team on github).

import {Page} from 'ionic-framework/ionic';
import {Inject} from 'angular2/core;
import {WpService} from './wpservice';

@Page({
  templateUrl: 'build/pages/home/home.html',
    providers: [WpService]
})
export class HomePage {
    static get parameters(){
       return [WpService];
      }
    constructor(wpservice) {
        this.service = wpservice;
        this.data = null;
    }

    retrieve() {
        this.service.loadData();
        setTimeout(() => {
            this.data = this.service.getData();
            console.log(this.data);
        }, 5000);
    }
}

And I changed the service file as below:

import {Injectable, Inject} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/Rx'

@Injectable()
export class WpService {
    static get parameters(){
      return [Http];
      }
    constructor(http) {
        this.http = http;
        this.data = null;
    }

    loadData() {
        this.http.get('<some rest api>').subscribe(data => {
            this.data = data.json() 
        });
        }

    getData() {
        return this.data;
    }
    }

Incase you decide to inject more than one service then you need to give the return statement in the get parameters method like below:

return [[service1],[service2]];

Hope this helps someone else facing the same issue. Thanks.




回答2:


I'm facing the same issue, it seems like its an issue with Ionic Version: 2.0.0-beta.1, apparently its creating typescript files with the extension of .js

read more about this issue here



来源:https://stackoverflow.com/questions/35667812/ionic-2-services-unexpected-token-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!