Ionic
gulullu
V2EX  ›  Ionic

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

  •  
  •   gulullu · Feb 24, 2017 · 8612 views
    This topic created in 3368 days ago, the information mentioned may be changed or developed.

    基类:

    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 replies    2017-03-03 14:16:23 +08:00
    bgies
        1
    bgies  
       Mar 3, 2017
    Change your constuctor to public or private... what happens?

    constructor(public accountData: AccountData, public navCtrl: NavController, ) {
    super(accountData, navCtrl);
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2978 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 07:29 · PVG 15:29 · LAX 00:29 · JFK 03:29
    ♥ Do have faith in what you're doing.