简体   繁体   English

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

[英]Type 'string' is not assignable to type 'number'. when building the application in production mode in angular7

I am working on an Angular7 application which works fine in developement mode.But when i try to create a production version i am getting an error as like this. 我正在开发在开发模式下可以正常工作的Angular7应用程序,但是当我尝试创建生产版本时却出现了这样的错误。

error TS2322: Type 'string' is not assignable to type 'number'.
src/app/video/video.component.ts(56,35): error TS2339: Property 'videoUrl' does not exist on type 'number | VideoName | (() => string) | { (...items: ConcatArray<VideoName>[]): VideoName[]; (...items: (VideoName | ConcatArray<VideoName>)[]): VideoName[]; } | ((searchElement: VideoName, fromIndex?: number) => number) | ... 25 more ... | (() => IterableIterator<...>)'.
  Property 'videoUrl' does not exist on type 'number'.

The code which the error show is here: 错误显示的代码在这里:

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

Though i have used an model for type 虽然我使用了模型作为类型

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

Full Code: 完整代码:

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() {
  }

}

Can anyone help me where i am wrong. 谁能帮助我我错了。

@Nidhin Kumar, change your class something like below, @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;
    }
}

this approach will take both string or number as type of the variable. 这种方法将stringnumber作为变量的类型。

UPDATE 更新

change this 改变这个

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

like below 像下面

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

A possible solution according to me should be convertin url to string whatever it may be so 根据我的可能的解决方案应该是将url转换为字符串,无论如何

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

Might help you fix the issue 可以帮助您解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “x”类型的参数不能分配给 Angular7 中“x”类型的参数 - Argument of type 'x' is not assignable to parameter of type 'x' in Angular7 类型&#39;字符&#39;不能指定为&#39;数字&#39;类型 - Type 'string' is not assignable to type 'number' “数字”类型不能分配给“字符串”类型 - Type 'number' is not assignable to type 'string' 类型字符串不可分配给类型编号 - Type string is not assignable to type number Angular:TS2322:类型“数字”不可分配给类型“字符串” - Angular: TS2322: Type 'number' is not assignable to type 'string' 错误 TS2322:类型“数字”不可分配给 angular 中的类型“字符串” - error TS2322: Type 'number' is not assignable to type 'string' in angular “数字”类型的参数不能分配给 angular2 中“字符串”类型的参数。 - Argument of type 'number' is not assignable to parameter of type 'string' in angular2. &#39;number&#39; 类型的 Angular Argument 不能分配给 &#39;string | 类型的参数不明确的&#39; - Angular Argument of type 'number' is not assignable to parameter of type 'string | undefined' “字符串”类型的 Angular 参数不可分配给“数字”类型的参数 - Angular argument of type 'string' is not assignable to parameter of type 'number' 如何解决 Angular 14 中的错误“类型'字符串'不可分配给类型'数字'”? - How to solve error "Type 'string' is not assignable to type 'number'" in Angular 14?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM