Angular 2 组件因添加空服务而中断

标签 angular angular2-services

在添加此服务之前,一切都运行良好。但一旦添加,即使它是空的,该组件也只会呈现一个空白页面(并破坏应用程序)。不会抛出任何错误。 删除 GuideService(以及 save(),因为它是依赖的)可以使应用程序再次完美运行。我缺乏经验...任何帮助都将不胜感激!

创建-guide.component.ts

import { GuideService } from '../guide/guide.service';
import { Component, OnInit } from '@angular/core';
import { Guide } from '../guide/guide.model';
import { Form, FormArray, FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';

@Component({
  selector: 'app-create-guide',
  templateUrl: './create-guide.component.html',
  styleUrls: ['./create-guide.component.css']
})
export class CreateGuideComponent implements OnInit {

  rform: FormGroup;

  constructor(private fb: FormBuilder, public guideService: GuideService) {
  }



  save(model: Form) {
    console.log(model);
    const newGuide = new Guide('', '', '', [], '');
    this.guideService.addGuide(newGuide)
      .subscribe(
        data => console.log(data),
        error => console.error(error)
      );

  }

  ngOnInit() {
    this.rform = this.fb.group({
      title : ['', Validators.compose([Validators.required])],
      description : ['', Validators.compose([Validators.required])],
      prereqs: ['', Validators.compose([Validators.required])],
      experienceLevel: ['', Validators.required],
      guideResources: this.fb.array([
        this.initGuideResource()
      ])

    });
  }

    initGuideResource() {
      return this.fb.group({
        resourceTitle: [null, Validators.compose([Validators.required])],
        resourceLink: [null, Validators.compose([Validators.required])],
        resourceTime: [null, Validators.compose([Validators.required])],
        resourceContent: [null, Validators.compose([Validators.required])]
      });
    }

    addGuideResource() {
      // add guide to the list
      const control = <FormArray>this.rform.controls['guideResources'];
      control.push(this.initGuideResource());
  }

    removeGuideResource(i: number) {
      // remove guide from the list
      const control = <FormArray>this.rform.controls['guideResources'];
      control.removeAt(i);
  }


}

指南.service.ts

import { Guide } from './guide.model';
import { Headers, Http, Response } from '@angular/http';
import {Injectable} from '@angular/core';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class GuideService {
    constructor(private http: Http) {}

    addGuide(guide: Guide) {
        const body = JSON.stringify(guide);
        const headers = new Headers({'Content-Type': 'application/json'});
        return this.http.post('http://localhost:3000/create', body, {headers: headers})
            .map((response: Response) => response.json())
            .catch((error: Response) => Observable.throw(error.json));
    }
}

最佳答案

代码看起来正确。根据个人经验,请确保将该服务作为提供者包含在 app.module.ts 文件中。在此链接中查看服务提供商 https://angular.io/guide/ngmodule

关于Angular 2 组件因添加空服务而中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624959/

相关文章:

angular - 重定向到 Angular 中的上一页

angular - 在 Angular 4/5+ 中从 API 获取图像?

angular - 无法绑定(bind)到 'placeholder',因为它不是 'ng-multiselect-dropdown' 的已知属性

node.js - Passport JWT : Cannot read property '_id' of undefined in MEAN

javascript - Angular - 在组件之间共享图像

angular - 使用 Kendo UI for Angular 以编程方式隐藏 GridComponent 中的列

javascript - 如何在 Angular 2/4 应用程序的 NgModule 中将对象传递给服务构造函数

Angular 2 Header组件标题根据状态动态变化

angular - 使用表单数组中另一个控件的值的最大验证器值

angular - 为 Angular 2 http 请求设置基本 url