首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'string‘不可赋值给类型'number’。在angular7中以生产模式构建应用程序时

类型'string‘不可赋值给类型'number’。在angular7中以生产模式构建应用程序时
EN

Stack Overflow用户
提问于 2019-02-07 18:17:44
回答 2查看 4.5K关注 0票数 0

我正在开发一个在mode.But开发中运行良好的Angular7应用程序,当我试图创建一个生产版本时,我得到了一个错误,如下所示。

代码语言:javascript
复制
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'.

错误显示的代码如下:

代码语言:javascript
复制
 Object.keys(this.quality).map((key) => {
    this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url});
  });

尽管我已经使用了一个模型来表示类型

代码语言:javascript
复制
export class VideoName {
id: number;
name?: string;
videoUrl?: string;
}

完整代码:

代码语言:javascript
复制
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() {
  }

}

有人能帮我指出我哪里错了吗?

EN

回答 2

Stack Overflow用户

发布于 2019-02-07 18:27:06

@Nidhin Kumar,更改您的类,如下所示

代码语言:javascript
复制
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作为变量的类型。

更新

更改此

代码语言:javascript
复制
this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url});

喜欢下面的

代码语言:javascript
复制
this.departments.push(new VideoName(key,this.quality[key].name,this.quality[key].url));
票数 0
EN

Stack Overflow用户

发布于 2019-02-07 18:31:07

根据我的说法,一种可能的解决方案应该是将url转换为字符串,无论它是什么。

代码语言:javascript
复制
this.departments.push({id: key, name: this.quality[key].name, videoUrl: this.quality[key].url+''});

可能会帮助你解决这个问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54571047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档