V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Ionic
gulullu
V2EX  ›  Ionic

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

  •  
  •   gulullu · 2017-02-24 15:15:00 +08:00 · 6710 次点击
    这是一个创建于 2814 天前的主题,其中的信息可能已经有所发展或是发生改变。

    基类:

    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 ()

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

    1 条回复    2017-03-03 14:16:23 +08:00
    bgies
        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);
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2837 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 02:06 · PVG 10:06 · LAX 18:06 · JFK 21:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.