无法在angularjs中解决此错误

时间:2018-01-18 19:33:15

标签: angular

OnSubmitAddVideo()包含错误!

“错误TS2345:类型'()=>任何'的参数不能分配给'Video'类型的参数。   '()=>类型中缺少属性'_id'任何'。 “

import  {Video} from './../video';
import { Component, OnInit } from '@angular/core';
import { VideoService } from './../video.service';
import { fail } from 'assert';

@Component({
  selector: 'app-video-center',
  templateUrl: './video-center.component.html',
  styleUrls: ['./video-center.component.css'],
  providers: [VideoService]
})
export class VideoCenterComponent implements OnInit {

  videos: Array<Video>;

  selectedVideo: Video;
  private hideNewVideo: boolean=true;
  constructor(private _videoService:VideoService) { }

  ngOnInit() {
    this._videoService.getVideos().subscribe(resVideoData=>this.videos=resVideoData);
  }

  newVideo(){
    this.hideNewVideo=false;
  }

  onSelectVideo(video:any){
    this.selectedVideo=video;
    this.hideNewVideo=true;
    console.log(this.selectedVideo);
  }

  onSubmitAddVideo(video : Video){
    this._videoService.addVideo(video).subscribe(resNewVideo => {
      this.videos.push(resNewVideo);                             
      this.hideNewVideo = true;
      this.selectedVideo = resNewVideo;
    });
  }

this._videoService.addVideo()的代码可在以下代码中找到

video.service.ts

 import { Injectable } from '@angular/core';
 import { Http,Response,Headers,RequestOptions} from '@angular/http';
 import 'rxjs/add/operator/map';
 import { Video } from './video';
 @Injectable()
 export class VideoService {

   private _getUrl="/api/videos";
   private _postUrl="/api/video";
   constructor(private _http: Http) { }

   getVideos(){
     return this._http.get(this._getUrl).map((response:Response)=>response.json());
   }

   addVideo(video:Video){
     let headers = new Headers({'Content-Type':'application/json'});
     let options = new RequestOptions({headers:headers});
     return this._http.post(this._postUrl,JSON.stringify(video),options).map((response:Response)=>response.json);
   }
 }

Image

0 个答案:

没有答案