ionic 2 如何继承一个已经依赖注入 service 的基类?

2017-02-24 15:15:00 +08:00
 gulullu

基类:

import { LoginPage } from './../login/login/login';
import { AccountData } from './../../storages/account_data';
import { Component, Injectable } from '@angular/core';
import { NavController, NavParams, Platform, ViewController } from 'ionic-angular';
/*
  Generated class for the Base page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/

@Component({
  selector: 'page-base',
  templateUrl: 'base.html'
})
export abstract class BasePage {
  needLogin = false;

  constructor(protected accountData: AccountData, protected nav: NavController) { }

  push(page: any) {
    this.accountData.hasLoggedIn().subscribe(hasLoggedIn => {
      if (hasLoggedIn) {
        this.nav.push(page);
      } else {
        if (page.needLogin) {
          this.nav.push(LoginPage);
        } else {
          this.nav.push(page)
        }
      }
    });
  }

  pop() {
    this.nav.pop();
  }

  setRoot(page: any) {
    this.nav.setRoot(page);
  }
}

继承该基类的:

import { AccountData } from './../../storages/account_data';
import { BasePage } from './../base/base';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

/*
  Generated class for the Discover page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-discover',
  templateUrl: 'discover.html'
})
export class DiscoverPage extends BasePage {

  constructor(protected accountData: AccountData, protected navCtrl: NavController, ) {
    super(accountData, navCtrl);
  }

}

运行ionic serve后报错: Uncaught TypeError: Object prototype may only be an Object or null: undefined at setPrototypeOf ()

但是如果父类的构造方法为空的话一切正常,求解。

6797 次点击
所在节点    Ionic
1 条回复
bgies
2017-03-03 14:16:23 +08:00
Change your constuctor to public or private... what happens?

constructor(public accountData: AccountData, public navCtrl: NavController, ) {
super(accountData, navCtrl);
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/342928

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX