Javascript “什么意思?”;“任何[]”;在js/ts数组中?
我现在正在学习英语。在第一课中,我读到我必须将数据保存到“any”数组中,然后保存到示例中的代码如下:Javascript “什么意思?”;“任何[]”;在js/ts数组中?,javascript,angular,typescript,Javascript,Angular,Typescript,我现在正在学习英语。在第一课中,我读到我必须将数据保存到“any”数组中,然后保存到示例中的代码如下: import { Component } from '@angular/core'; import { GithubService } from '../../services/github.service'; @Component({ selector: 'profile', template: `<h1>Profile Component</h1>`,
import { Component } from '@angular/core';
import { GithubService } from '../../services/github.service';
@Component({
selector: 'profile',
template: `<h1>Profile Component</h1>`,
})
export class ProfileComponent {
user:any[];
constructor(private _githubService:GithubService){
// this._githubService.getUser().subscribe(user => {console.log(user)});
this._githubService.getUser().subscribe(user => {
this.user = user;
});
}
}
从'@angular/core'导入{Component};
从“../../services/github.service”导入{GithubService};
@组成部分({
选择器:“配置文件”,
模板:`Profile Component`,
})
导出类ProfileComponent{
用户:任何[];
构造函数(私有的_githubService:githubService){
//这是.\u githubService.getUser().subscribe(用户=>{console.log(用户)});
这是。_githubService.getUser().subscribe(用户=>{
this.user=用户;
});
}
}
什么是
用户:任意[]代码>?我试着在谷歌的github上搜索,但什么也没找到。不知道事件要读什么。首先要知道的是any[]
只是Typescript中的有效变量声明,而不是Javascript中的有效变量声明
它是一个由两部分组成的声明-[]
在Typescript中用于声明一个值数组。any
用于声明数组中的条目可以是任何类型。相反,只能为数组指定一种有效类型。例如,string[]
将声明string
对象的数组。然后,Typescript编译器将检查您是否只向数组添加字符串对象。用户:任意代码>意味着我们可以存储字符串、数字、对象、布尔等类型的变量
用户:任意[]
表示用户
必须是数组,数组元素可以有类型字符串、数字、s对象、布尔值等。使用任何
通常表示缺少适当的类型。因此,我强烈建议定义一个描述用户对象的接口User
甚至,在这种情况下,使用一些准备好的类型化API,比如它意味着一个由任何
类型元素组成的数组,这意味着如果需要,我可以将字符串和对象保存在一个数组中?@NikitaM是的,您可以将任何内容存储为数组的元素any[]
只说它是一个包含未知元素的数组。