我是新手,我尝试通过使用服务将一些formdata从一个组件发送到另一个组件,但是它遇到了一些问题,即,它将错误消息显示为
错误TS2339:类型'((data:any)=> void')不存在属性'subscribe'。
这是我拥有数据的组件
import { Component, OnInit } from '@angular/core';
import { JarwisService } from '../../Services/jarwis.service';
import { DataTransferService } from '../../dt-broker/dt-core/services/data-transfer.service';
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { environment } from 'environments/environment';
import { NgForm } from '@angular/forms';
declare var $: any;
@Component({
selector: 'app-addlist',
templateUrl: './addlist.component.html',
styleUrls: ['./addlist.component.css']
})
export class AddlistComponent implements OnInit {
constructor(private Jarwis:JarwisService,
private dataTrans: DataTransferService,
private router : Router,
private http: HttpClient,
private _router : Router,
private activatedRoute: ActivatedRoute) { }
preview(form: NgForm){
this.Jarwis.getpreviewcontent(form.value).subscribe(
viewdata => this.handlepreviewResponse(viewdata)
);
}
handlepreviewResponse(data){
this.dataTrans.setpreviewdata(data);
this.router.navigate(['/previewad']);
}
}
这是我要发送数据的组件
import { Component, OnInit } from '@angular/core';
import { DataTransferService } from '../../../dt-broker/dt-core/services/data-transfer.service';
@Component({
selector: 'ngx-previewad',
templateUrl: './previewad.component.html',
styleUrls: ['./previewad.component.scss']
})
export class PreviewadComponent implements OnInit {
constructor(private dataTrans: DataTransferService) { }
public result=null;
ngOnInit() {
// console.log(this.dataTrans.setpreviewdata);
//alert(this.result);
this.dataTrans.setpreviewdata.subscribe(message => this.result = message);
}
}
这是我为传输数据而编写的服务
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataTransferService {
private previewdata = new BehaviorSubject('no data');
datapreview = this.previewdata.asObservable();
constructor() { }
setpreviewdata(data){
return this.previewdata.next(data);
//console.log(this.datapreview);
}
}
任何帮助都是有意义的。
答案 0 :(得分:1)
我可以在这里注意到多个错误。
首先要注意的是BehaviorSubject.next
是void函数。这就是为什么您会收到此错误,并且正在订阅不为更改设置的值的原因。
据我了解,您在这里不需要BehaviorSubject
。请改用EventEmitter
。
您的服务将具有如下形式。
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataTransferService {
public previewdata = new EventEmitter();
}
发送数据如下
----- bunch of code ----
export class AddlistComponent implements OnInit {
constructor(private Jarwis:JarwisService,
private dataTrans: DataTransferService,
private router : Router,
private http: HttpClient,
private _router : Router,
private activatedRoute: ActivatedRoute) { }
preview(form: NgForm){
this.Jarwis.getpreviewcontent(form.value).subscribe(
viewdata => this.handlepreviewResponse(viewdata)
);
}
handlepreviewResponse(data){
this.dataTrans.previewdata.emit(data);
this.router.navigate(['/previewad']);
}
}
按以下方式使用订阅接收数据。
--- bunch of code ---
export class PreviewadComponent implements OnInit {
constructor(private dataTrans: DataTransferService) { }
public result=null;
ngOnInit() {
this.dataTrans.previewdata.subscribe(message => this.result = message);
}
}
享受!
答案 1 :(得分:0)
您是将BehaviorSubject声明为私有而不是public。
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataTransferService {
public previewdata = new BehaviorSubject('no data');
datapreview = this.previewdata.asObservable();
constructor() { }
setpreviewdata(data){
this.previewdata.next(data);
//console.log(this.datapreview);
}
getpreviewMessage(): Observable<any> {
return this.previewdata.asObservable();
}
}
您要获取数据的组件
import { Component, OnInit } from '@angular/core';
import { DataTransferService } from '../../../dt-broker/dt-
core/services/data-transfer.service';
@Component({
selector: 'ngx-previewad',
templateUrl: './previewad.component.html',
styleUrls: ['./previewad.component.scss']
})
export class PreviewadComponent implements OnInit {
constructor(private dataTrans: DataTransferService) { }
public result=null;
ngOnInit() {
// console.log(this.dataTrans.setpreviewdata);
//alert(this.result);
this.dataTrans.getpreviewMessage.subscribe(message => this.result =
message);
}
}
答案 2 :(得分:0)
最后我找到了答案。是我的错,我在视图组件中从服务中调用了错误的功能
export class PreviewadComponent implements OnInit {
constructor(public dataTrans: DataTransferService) { }
public result=null;
ngOnInit() {
this.dataTrans.datapreview.subscribe(message => this.result = message);
// console.log(this.result);
}
}