【问题标题】:Angular 6: subscribe does not exist on type voidAngular 6: void 类型不存在订阅
【发布时间】:2018-11-30 00:29:19
【问题描述】:

我想在提交表单后向用户显示成功消息。

表单在提交数据时工作正常,但没有显示 flash 消息,我收到错误:在 void 类型上不存在订阅

这是我所做的,

HTML:

<form [formGroup]="angForm" novalidate>
        <div class="form-group">
          <label class="col-md-4">Comment author</label>
          <input type="text" class="form-control" name="author" formControlName="author" #author />
        </div>
        <div *ngIf="angForm.controls['author'].invalid && (angForm.controls['author'].dirty || angForm.controls['author'].touched)"
          class="alert alert-danger">
          <div *ngIf="angForm.controls['author'].errors.required">
            Name is required.
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-4">comment description</label>
          <input type="text" class="form-control" formControlName="description" #description/>
        </div>
        <div *ngIf="angForm.controls['description'].invalid && (angForm.controls['description'].dirty || angForm.controls['description'].touched)"
          class="alert alert-danger">
          <div *ngIf="angForm.controls['description'].errors.required">
            description is required.
          </div>
        </div>
        <div class="form-group">
          <button (click)="addReview(author.value, description.value)" [disabled]="angForm.pristine || angForm.invalid" class="btn btn-primary">Add</button>
        </div>
      </form>

component.ts:

import { Component, OnInit } from '@angular/core';
import { MoviesService } from '../movies.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { FlashMessagesModule, FlashMessagesService } from 'angular2-flash-messages';
@Component({
  selector: 'app-movie',
  templateUrl: './movie.component.html',
  styleUrls: ['./movie.component.scss']
})
export class MovieComponent implements OnInit {
  angForm: FormGroup;
  // tslint:disable-next-line:max-line-length
  constructor(
    private flashMessages: FlashMessagesService,
    private fb: FormBuilder,
    private route: Router,
    private http: HttpClient,
    private activeRouter: ActivatedRoute,
    private moviesService: MoviesService) {
    this.createForm();
  }
  createForm() {
    this.angForm = this.fb.group({
      author: ['', Validators.required],
      description: ['', Validators.required]
    });
  }
  addReview(author, description) {
    this.moviesService.addReview(author, description).subscribe(data => {
      if (data.sucess) {
        this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
        this.route.navigate(['/movie']);
      } else {
        this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
        this.route.navigate(['/movie']);
      }
    });
  }
  ngOnInit() {
  }
}

这里服务 .ts 一个

addReview(author, description) {
    const uri = 'http://localhost:8000/movies/comments/';
    const obj = {
      author: author,
      description: description
    };
    this
      .http
      .post(uri, obj)
      .subscribe(res =>
          console.log('Done'));
  }

这是应用程序的 html 组件。

<div class="container">
  <flash-messages></flash-messages>
  <router-outlet></router-outlet>
</div>

问题 我的代码有什么问题?这是显示闪存消息的正确方法吗?我被困在这里,任何帮助都会很棒

【问题讨论】:

    标签: angular typescript observable


    【解决方案1】:

    您的服务中的addReview() 方法没有返回任何内容。无需订阅服务中的 post() 调用,只需返回 post() 方法即可。这将使该方法返回一个可观察的对象,您可以在调用它时订阅它:

    addReview(author, description) {
        const uri = 'http://localhost:8000/movies/comments/';
        const obj = {
            author: author,
            description: description
        };
        return this.http.post(uri, obj);
    }
    

    注意:您需要更改处理订阅服务调用的方式。您可以在the angular guide 中阅读更多关于它是如何工作的信息。您的服务调用应更改为:

    this.moviesService.addReview(author, description).subscribe(success => {
        this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
        this.route.navigate(['/movie']);
    }, error => {
        this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
        this.route.navigate(['/movie']);
    });
    

    【讨论】:

    • 改成之后我得到这个错误:property 'success' does not exist on type 'response '?
    • @Kaczkapojebana,我添加了一个 sn-p 代码来展示如何正确订阅服务调用
    • 更改后返回此错误:找不到名称作者或描述,我尝试将它们声明为公共等但仍然相同,我在这里缺少什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2019-01-03
    • 1970-01-01
    • 2017-12-25
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多