我有2个兄弟组件,我在一个组件中做一个http请求,如果发生特定情况,它应该发出另一个http请求,写在另一个组件中.所以我应该能够在第一个组件中调用该方法.
这是第一个组成部分:
import { Component,OnInit,Inject } from '@angular/core'; import { Http } from '@angular/http'; import { SendCardComponent } from '../send-card/send-card.component'; @Component({ selector: 'app-input-field',templateUrl: './input-field.component.html',styleUrls: ['./input-field.component.css'],}) export class InputFieldComponent implements OnInit { value = ''; output = ''; @Inject(SendCardComponent) saro: SendCardComponent; constructor(private http : Http) { } onEnter(value: string) { this.value = value; this.http.post('http://localhost:5000/APIconversation/',{"val":value}) .map(response=>response.json()) .subscribe( data => { this.output = data.result.fulfillment.speech,if(data.result.fulfillment.speech == 'test'){ saro.sendCard('done','1' ); } }); }
我试图从InputFieldComponent调用sendCardComponent中定义的sendCard(),如下所示:
import { Component,OnInit } from '@angular/core'; import { Http } from '@angular/http'; @Component({ selector: 'app-send-card',templateUrl: './send-card.component.html',styleUrls: ['./send-card.component.css'] }) export class SendCardComponent implements OnInit { constructor(private http : Http) { } ngOnInit() { } output = ''; sendCard(value:string,id:number){ this.http.post('http://localhost:5000/APIconversation/',{"val":value}) .map(response=>response.json()) .subscribe( data => { this.output = data.result.fulfillment.messages[1].payload.options[id].type = $('#'+(id+1)+'>span').html(); }); } //sendCard }
调用saro.sendCard时出错:
[ts] cannot find name ‘saro’
我究竟做错了什么?
解决方法
在InputFieldComponent中创建SendCardComponent的实例
import { Http } from '@angular/http'; import { SendCardComponent } from '../send-card/send-card.component'; export class InputFieldComponent{ //your other variables and methods constructor(private http : Http) { } let saro = new SendCardComponent(this.http); saro.sendCard() }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。