[英]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. 这种方法将
string
或number
作为变量的类型。
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.