“字符串”类型不可分配给“数字”类型。在angular7中以生产模式构建应用程序时

时间:2019-02-07 10:17:44

标签: angular typescript

我正在开发一个在开发模式下可以正常工作的Angular7应用程序。但是,当我尝试创建生产版本时,出现了这样的错误。

 Object.keys(this.quality).map((key) => {
    this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url});
  });

错误显示的代码在这里:

export class VideoName {
id: number;
name?: string;
videoUrl?: string;
}

尽管我使用了一个类型的模型

import { Component, OnInit, SimpleChanges, ElementRef, ViewChild } from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import { VideoName } from './model';
import { find } from 'lodash';



@Component({
  selector: 'app-video',
  templateUrl: './video.component.html',
  styleUrls: ['./video.component.scss']
})
export class VideoComponent implements OnInit {

  @ViewChild('videoPlayer') videoplayer: ElementRef;

  departments?: VideoName[] = [];

  public videoPath: string;
  public title: string;
  public plot: string;
  public englishTitle: string;
  public tamilTitle: string;
  public posterImage: string;
  public quality: string;

  constructor(private route: ActivatedRoute, private elRef: ElementRef) {
    this.route.queryParams.subscribe(params => {
      console.log('params is', params);
      this.videoPath = params["url"];
      this.title = params["title"];
      this.plot = params["plot"];
      this.englishTitle = params["subtitles_en"];
      this.tamilTitle = params["subtitles_ta"];
      this.posterImage = params["poster"];
      this.quality = JSON.parse(params["quality"]);
      Object.keys(this.quality).map((key) => {
        this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url});
      });
    });

  }

  changedata($event){
    let id = $event.target.value;
    let selectedData = find(this.departments, {id: id});
    this.videoPath = selectedData.videoUrl;
    this.videoplayer.nativeElement.load();
    this.videoplayer.nativeElement.play();
  }

  ngOnInit() {
  }

}

完整代码:

compileSdkVersion 28

有人在我错的地方帮我

2 个答案:

答案 0 :(得分:0)

@Nidhin Kumar,如下更改您的课程,

export class VideoName {
    id: number;
    name?: string | number;
    videoUrl?: string | number;

    constructor(id,name,videoUrl){
      this.id=id;
      this.name=name;
      this.videoUrl= videoUrl;
    }
}

此方法将同时使用stringnumber作为变量的类型。

更新

  

更改此

this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url});
  

如下所示

this.departments.push(new VideoName(key,this.quality[key].name,this.quality[key].url));

答案 1 :(得分:0)

对我来说,可能的解决方案应该是将url转换为字符串,无论如何

this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url+''});

可以帮助您解决问题