简体   繁体   English

将Javascript成员变量转换为Typescript成员变量

[英]Converting Javascript member variable to Typescript member variable

I am trying to work out how to convert the member variables of my Javascript code to the Typescript equivalent. 我正在尝试找出如何将Javascript代码的成员变量转换为Typescript等效项。

In my Javascript code, after the constructor() I have: 在我的Javascript代码中,在constructor()之后,我有:

this.theMediaItem = [];
this.theMediaItem.embedLink = '';
this.theMediaItem.username = '';

I have tried the following as the Typescript equivalent, inserted as required between the export class and the constructor() but it doesn't like the '.': 我已经尝试了以下内容作为Typescript等效项,并根据需要将其插入了export classconstructor()但它不喜欢'。':

theMediaItem = [];
theMediaItem.embedLink = '';
theMediaItem.username = '';

For what you are describing, here's one typescript equivalent: 对于您要描述的内容,这里有一个等效的打字稿:

interface MediaItemArray<T> extends Array<T> {
    embedLink?: string;
    username?: string;
}

class MyClass {
    theMediaItem: MediaItemArray<any> = [];

    constructor() {
        this.theMediaItem.embedLink = "";
        this.theMediaItem.username = "";
    }
}

That's kind of strange though... you probably don't want to use an array and it's best to just have properties directly on the class: 但是,这有点奇怪……您可能不想使用数组,最好直接在类上具有属性:

class MediaItem {
    embedLink = "";
    username = "";
}

Or described in an interface: 或在界面中描述:

interface MediaItem {
    embedLink: string;
    username: string;
}

Then, as you say in the comments, if you have a view you can add it as a property like so: 然后,如您在评论中所述,如果您有视图,则可以将其添加为属性,如下所示:

class MyView {
    mediaItem = new MediaItem()
}

Or if you are using an interface: 或者,如果您使用的是界面:

class MyView {
    mediaItem: MediaItem = {
        embedLink: "",
        username: ""
    };
}

You need to use syntax below, also make sure you don't define your theMediaItem as array, because from usage I see that you assign its properties: 您需要使用以下语法,还请确保不要将theMediaItem定义为数组,因为从使用情况看,您将为其分配属性:

class YourClass {
    constructor() {
       this.theMediaItem = {};
       this.theMediaItem.embedLink = '';
       this.theMediaItem.username = '';
    }
}

or do it in simple way: 或以简单的方式做到这一点:

class YourClass {
    theMediaItem = { embedLink: '', username: '' },    
    constructor() {
       // ...
    }
}

https://www.typescriptlang.org/docs/tutorial.html https://www.typescriptlang.org/docs/tutorial.html

Did you try the 'dictionary' pattern? 您尝试过“词典”模式吗? Please refer the TS document and wiki page below. 请参考下面的TS文档和Wiki页面。

interface Dictionary { [index: string]: string; 接口Dictionary {[index:string]:string; } }

https://typescript.codeplex.com/wikipage?title=Interfaces%20in%20TypeScript&referringTitle=TypeScript%20Documentation https://typescript.codeplex.com/wikipage?title=接口%20in%20TypeScript&referringTitle = TypeScript%20Documentation

https://en.wikipedia.org/wiki/Associative_array#Example https://zh.wikipedia.org/wiki/Associative_array#Example

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM