简体   繁体   English

angular 2如何自定义界面,如onInit

[英]angular 2 how to custom interface like onInit

Here I have a menu service like below... 在这里,我有如下菜单服务...

menu.service.ts menu.service.ts

    import { Injectable, EventEmitter } from '@angular/core';
        import {
            Http,
            Request,
            Response,
            RequestMethod,
            Headers,
            URLSearchParams,
            RequestOptions,
            ResponseContentType,
        } from '@angular/http';
        import { Observable } from 'rxjs/Observable';
        import * as _ from 'lodash'

        @Injectable()
        export class MenuService {

            constructor(public http: Http) {}
            IsOnReady = false;
            _onReady: EventEmitter<string> = new EventEmitter<string>(true);

            data = [];
            getData() {

            return  this.http.get('/api/Menus').subscribe(data => {      
                    this.data = data.json()
                    this.IsOnReady = true;
                    this._onReady.emit('menu is ready');
                });

            }


            onReady(callback) {
                if (this.IsOnReady) {
                    callback();
                }
                else {
                    this._onReady.subscribe(r => {
                        callback();
                    });
                }
            }
        }

In another page, I always need to call menu.onReady to get menu data, then do something after... 在另一页中,我总是需要调用menu.onReady来获取菜单数据,然后在执行后...

import { OnInit } from '@angular/core';
import { MenuService } from '../../../services/menu.service';


export class NewsComponentBase implements OnInit{

    NewsCategoryID:string

    constructor(public menu: MenuService) {
    }

    ngOnInit() {
        this.menu.onReady(() => this.active());
    }

    active() {
        this.NewsCategoryID= this.menu.data[0].NewsCategoryID;
    }
}

How to achieve an interface like angular's onInit , some code like 如何实现像angular的onInit这样的接口,一些代码例如

import { MenuService,MenuOnReady} from '../../../services/menu.service';

export class NewsComponentBase implements MenuOnready {

    NewsCategoryID:string

    constructor(public menu: MenuService) {
    }

    MenuOnReady () {
        this.NewsCategoryID= this.menu.data[0].NewsCategoryID;
    }
}

I think you are not thinking the 'angular 2 way' 我认为您不是在考虑“角度2方式”

If we follow the official guide https://angular.io/docs/ts/latest/tutorial/toh-pt4.html 如果我们遵循官方指南https://angular.io/docs/ts/latest/tutorial/toh-pt4.html

Your method getData should return a promise or an observable and you have to subscribe in the ngOnInit method. 您的方法getData应该返回一个promise或一个observable,并且您必须订阅ngOnInit方法。

Your code should be something like this: 您的代码应如下所示:

 import { Injectable, EventEmitter } from '@angular/core';
        import {
            Http,
            Request,
            Response,
            RequestMethod,
            Headers,
            URLSearchParams,
            RequestOptions,
            ResponseContentType,
        } from '@angular/http';
        import { Observable } from 'rxjs/Observable';
        import * as _ from 'lodash'

        @Injectable()
        export class MenuService {

            constructor(public http: Http) {}

            getData():Promise<any> {
                return  this.http.get('/api/Menus');
            }
        }

import { OnInit } from '@angular/core';
import { MenuService } from '../../../services/menu.service';


export class NewsComponentBase implements OnInit{

    NewsCategoryID:string

    constructor(public menu: MenuService) {
    }

    ngOnInit() {
        this.menu.getData().then(data => {      
                    this.data = data.json()
                   this.active();
                });
    }

    active() {
        this.NewsCategoryID= this.menu.data[0].NewsCategoryID;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM