Javascript Angular-从方法调用返回的observable管道,其中包含条件-无法读取属性';订阅';未定义的
我正在尝试通过管道从组件的save方法返回可观测值 在这个基于条件的save方法中,会打开一个对话框,等待用户输入,然后再调用update端点并将结果作为可观察结果返回,最终由save()方法返回 问题是,我试图通过管道传递save()的结果,以检查值是否已发出,然后离开组件。因为.subscribe()在返回值之前正在执行 代码摘要如下所示:Javascript Angular-从方法调用返回的observable管道,其中包含条件-无法读取属性';订阅';未定义的,javascript,angular,typescript,Javascript,Angular,Typescript,我正在尝试通过管道从组件的save方法返回可观测值 在这个基于条件的save方法中,会打开一个对话框,等待用户输入,然后再调用update端点并将结果作为可观察结果返回,最终由save()方法返回 问题是,我试图通过管道传递save()的结果,以检查值是否已发出,然后离开组件。因为.subscribe()在返回值之前正在执行 代码摘要如下所示: save() : Observable<Address> { let value = this.form.get('Address').va
save() : Observable<Address> {
let value = this.form.get('Address').value
if (JSON.stringify(this.address) != JSON.stringify(value.Address)) {
const ref = this.dialog.open(AddressChangeDialog);
ref.componentInstance.canSave = this.form.valid;
ref.afterClosed().pipe(map(result => {
if (result) {
switch (result) {
case AddressChangeDialog.Save:
//Save the form (returns the observable here)
return this.addressService.put(value)
case AddressChangeDialog.Discard:
// Cancel the save
break;
default:
break;
}
}
}));
}
else {
// if the address hasnt changed just save and return the observable
return this.addressService.put(value)
}
}
我遇到的问题是,当地址更改并打开对话框时,.subscribe()上出现错误,因为save()方法尚未返回任何内容
任何帮助都将不胜感激,因为我已经挠头好一阵子了
谢谢 使用map()
时,始终必须返回一些内容。有好几种情况下你什么都不退。至少返回null
save() : Observable<Address> {
let value = this.form.get('Address').value
if (JSON.stringify(this.address) != JSON.stringify(value.Address)) {
const ref = this.dialog.open(AddressChangeDialog);
ref.componentInstance.canSave = this.form.valid;
ref.afterClosed().pipe(map(result => {
if (result) {
switch (result) {
case AddressChangeDialog.Save:
//Save the form (returns the observable here)
return this.addressService.put(value)
case AddressChangeDialog.Discard:
// Cancel the save
// no return value
break;
// no return value
default:
break;
}
}
// no return value
// You always have to return something. So here we
// return null in case no other return is called before
return null;
}));
}
else {
// if the address hasnt changed just save and return the observable
return this.addressService.put(value)
}
}
save():可观察{
让value=this.form.get('Address').value
if(JSON.stringify(this.address)!=JSON.stringify(value.address)){
const ref=this.dialog.open(AddressChangeDialog);
ref.componentInstance.canSave=this.form.valid;
参考afterClosed()管道图(结果=>{
如果(结果){
开关(结果){
案例地址更改对话框。保存:
//保存表单(在此处返回可观察值)
返回此.addressService.put(值)
案例地址更改对话框。放弃:
//取消保存
//无返回值
打破
//无返回值
违约:
打破
}
}
//无返回值
//你总是要归还一些东西,所以我们来了
//如果之前没有调用其他返回,则返回null
返回null;
}));
}
否则{
//如果地址没有更改,只需保存并返回可观察的
返回此.addressService.put(值)
}
}
save方法不会返回任何内容。Hi@MoxxiManagarm我提交的代码是一个摘要。我留下了一条评论来说明退货发生的地方。代码确实会返回,但我想指出的是,当出现地址更改时,它正在等待对话框在返回前由用户操作解决,但是它在返回前执行.subscribe()。我将问题更新为更清晰OK,但仍然是。如果不是这样的话。您可能希望返回afterClosed()可观察值<代码>返回参考afterClosed().管道(/*…*/)代码>啊,好吧,我没有意识到当使用开关地图时,你必须返回每一层。谢谢你的帮助!
save() : Observable<Address> {
let value = this.form.get('Address').value
if (JSON.stringify(this.address) != JSON.stringify(value.Address)) {
const ref = this.dialog.open(AddressChangeDialog);
ref.componentInstance.canSave = this.form.valid;
ref.afterClosed().pipe(map(result => {
if (result) {
switch (result) {
case AddressChangeDialog.Save:
//Save the form (returns the observable here)
return this.addressService.put(value)
case AddressChangeDialog.Discard:
// Cancel the save
// no return value
break;
// no return value
default:
break;
}
}
// no return value
// You always have to return something. So here we
// return null in case no other return is called before
return null;
}));
}
else {
// if the address hasnt changed just save and return the observable
return this.addressService.put(value)
}
}