Angular2无法解析ParamDecorator的所有参数:(?,?,?)

时间:2016-10-22 05:18:24

标签: angular typescript dependency-injection angular-services

[解决]

我从JS API获取Vimeo数据oEmbed,所以我创建了一个服务:

video.service.ts

import {Injectable, Inject} from "@angular/core";
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/map';

import {Video} from "./video";

@Injectable()
export class VideoService {
  apiUrl = "https://vimeo.com/api/oembed.json?url="

  constructor(private _http: Http){}

  getVideo(videoUrl:string){
    return this._http.get(this.apiUrl+videoUrl)
               .map(res => res.json());
  }
}

使用模型界面Video

video.ts

export interface Video {
  url: string,
  title: string,
  desc: string,
  thumbnail: string,
  player: string
}

之后,我将其注入VideoGridComponent

视频-grid.component.ts

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

@Component({
  selector: 'app-video-grid',
  templateUrl: './video-grid.component.html',
  styleUrls: ['./video-grid.component.css']
})

export class VideoGridComponent implements OnInit {
  urls: string[] = [
    "https://vimeo.com/110217114", 
    "https://vimeo.com/175629655", 
    "https://vimeo.com/187604216", 
    "https://vimeo.com/177237096"
  ];
  videos:Video[];

  constructor(private _videoService: VideoService) { }

  ngOnInit() {
    for (var i in this.urls) {
      this._videoService.getVideo(this.urls[i])
        .subscribe(
          data => this.formatRemoteVideo(data),
          error => console.log(error),
          () => console.log('Finish get video')
        )    
    }
  }

  formatRemoteVideo(data){
    let vid:Video;
    vid.title = data.title
    vid.desc = data.description
    vid.thumbnail = data.thumbnail_url
    vid.player = data.html
    this.videos.push(data)
  }
}

使用well结果完成的TS编译器没有错误。但浏览器不运行脚本并输出属于ParamDecorator的奇怪错误:

metadata_resolver.js:499 Uncaught Error: Can't resolve all parameters for ParamDecorator: (?, ?, ?)

我研究过,但似乎没有人遇到过。我哪里出错了?

更新 我还检查了tsconfig.json,它有正常值:

{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

更新2 我不知道为什么,但是在取消终端npm start并重新运行之后它就会运行。我认为这是一个编译器问题,它不会复制/更新新的声明,因此会出现此错误。谢谢大家!

0 个答案:

没有答案