使用不起作用的服务调用角度组件之间的方法

时间:2017-06-15 10:48:31

标签: angular angular2-services angular2-nativescript nativescript-telerik-ui injectable

我正在尝试使用可注入服务

从其他组件调用一个组件中的方法

我的第一个组件(我在此组件的其他组件中调用方法)

bottombar.component.ts

import { Component, ElementRef, OnInit, ViewChild, AfterViewInit, ChangeDetectorRef, EventEmitter, Input, Output } from "@angular/core";
import { Router } from "@angular/router";
import { Label } from "ui/label";
import { BottomBarService } from '../../services/bottombarservice/bottombar.service';

@Component({
  selector: "bottom-bar",
  templateUrl: "./components/bottombar/bottombar.html",
  styleUrls:["./components/bottombar/bottombar-common.css", "./components/bottombar/bottombar.css"],
  //providers: [BottomBarService]
})

export class bottombarComponent implements OnInit {

  constructor(private bottomBarService: BottomBarService) {

  }

  openDrawer(){
    this.bottomBarService.callSideDrawerMethod();
  }
  ngOnInit() {}


}

从上面的组件中我尝试调用 SideDrawerGettingStartedComponent 中的方法,该方法将在下面触发一个打开侧抽屉的方法

我的第二个组成部分(其中存在被叫方法)

sidedrawer.component.ts

import { Component, ViewChild, OnInit, AfterViewInit, ChangeDetectorRef } from "@angular/core";
import { Page } from "ui/page";
import { ActionItem } from "ui/action-bar";
import { Observable } from "data/observable";
import { RadSideDrawerComponent, SideDrawerType } from "nativescript-telerik-ui-pro/sidedrawer/angular";
import { RadSideDrawer } from 'nativescript-telerik-ui-pro/sidedrawer';
import { BottomBarService } from '../../services/bottombarservice/bottombar.service';

@Component({
    // moduleId: module.id,
    selector: "tk-sidedrawer-getting-started",
    templateUrl: "./components/sidedrawer/sidedrawer.html",
    styleUrls: ['./components/sidedrawer/sidedrawer.css'],
    //providers: [BottomBarService]
})
export class SideDrawerGettingStartedComponent implements AfterViewInit, OnInit {
    private _mainContentText: string;

    constructor(private _changeDetectionRef: ChangeDetectorRef,private bottomBarService: BottomBarService) {
      this.bottomBarService.sideDrawerMethodCalled$.subscribe(() => {
        console.log("subscribed")
        alert("hello")
        this.openDrawer()
      })
    }

    @ViewChild(RadSideDrawerComponent) public drawerComponent: RadSideDrawerComponent;
    private drawer: RadSideDrawer;

    ngAfterViewInit() {
        this.drawer = this.drawerComponent.sideDrawer;
        this._changeDetectionRef.detectChanges();
    }

    ngOnInit() {
        this.mainContentText = "SideDrawer for NativeScript can be easily setup in the HTML definition of your page by defining tkDrawerContent and tkMainContent. The component has a default transition and position and also exposes notifications related to changes in its state. Swipe from left to open side drawer.";
    }

    get mainContentText() {
        return this._mainContentText;
    }

    set mainContentText(value: string) {
        this._mainContentText = value;
    }

    public openDrawer() {
        console.log("triggered openDrawer in main component")
        this.drawer.showDrawer();
    }

    public onCloseDrawerTap() {
       this.drawer.closeDrawer();
    }
}

共享服务如下

bottombar.service.ts

import { Subject }    from 'rxjs/Subject';
import { Injectable } from '@angular/core';

@Injectable()
export class BottomBarService{
    private sideDrawerMethodSource = new Subject<any>();
    sideDrawerMethodCalled$ = this.sideDrawerMethodSource.asObservable();

    callSideDrawerMethod(){
        console.log("Inside service bottomBar")
        this.sideDrawerMethodSource.next(null);
        console.log("after")
    }
}

问题

我有一个与 bottombar.component.ts 关联的html,其中包含一个按钮。点按它会触发 bottombar.component.ts

中的openDrawer()函数

我可以在服务文件中看到安慰值,但不知何故它没有触发 sidedrawer.component.ts

中的订阅

没有错误,因此很难找到导致问题的原因。

此外,我已在 ngModule 的提供商中声明我的服务,以避免单身人士问题。

我有什么东西在这里失踪吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

您似乎错过了在'$'

旁边添加sideDrawerMethodCalled

尝试以下代码:

callSideDrawerMethod(){
        console.log("Inside service bottomBar")
        this.sideDrawerMethodSource$.next(null);
        console.log("after")
    }

答案 1 :(得分:0)

在您的共享服务中,您可以传递ASSERT( value < threshold, " The value is above the threshold ") ,而不是在null传递this.sideDrawerMethodSource.next(null)。在 bottombar.service.ts -

status:boolean

现在,在 bottombar.component.ts 中,在调用方法时传递状态 -

callSideDrawerMethod(status:boolean){
    this.sideDrawerMethodSource.next(status);
}

sidedrawer.component.ts 中,传递this.bottomBarService.callSideDrawerMethod(true); 参数 -

status

而不是在构造函数中订阅,而是将代码放在 this.bottomBarService.sideDrawerMethodCalled$.subscribe((status) => { console.log("subscribed") alert("hello") this.openDrawer() }) 生命周期钩子中。

希望这有帮助。