Javascript 如何在angular 2中返回多个值?
我想分别返回多个值,例如“firstname”和“secondname”。我该怎么做 我试图将它们作为字符串返回,但不起作用:Javascript 如何在angular 2中返回多个值?,javascript,angular,return-value,Javascript,Angular,Return Value,我想分别返回多个值,例如“firstname”和“secondname”。我该怎么做 我试图将它们作为字符串返回,但不起作用: import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
pageheader : string = 'text';
imagepath : string = 'https://3wga6448744j404mpt11pbx4-wpengine.netdna-ssl.com/wp-content/uploads/2018/11/Treehouse-Logo-Green-Large.png';
firstname : string ='Tom';
secondname : string =' Hopkins';
getfullName(): string
{
return this.firstname;
return this.secondname
}
例如,您不能在一个块中添加多个return语句,而是可以返回同时具有这两个值的对象,并在任何需要的地方使用它-
getfullName() {
return {firstName: this.firstname, lastName: this.secondname}
}
如果您想在
getFullName()
中使用全名,只需使用like
getfullName() {
return `${this.firstname} ${this.secondname}`
}
Javascript函数返回单个值,因此在本例中,可以返回
对象
,数组
,字符串
值
对象
getfullName() {
return {firstName: this.firstname, lastName: this.secondname}
}
getfullName() {
return [this.firstname,this.secondname];
}
getfullName() {
return `${this.firstname} ${this.secondname}`;
}
阵列
getfullName() {
return {firstName: this.firstname, lastName: this.secondname}
}
getfullName() {
return [this.firstname,this.secondname];
}
getfullName() {
return `${this.firstname} ${this.secondname}`;
}
字符串
getfullName() {
return {firstName: this.firstname, lastName: this.secondname}
}
getfullName() {
return [this.firstname,this.secondname];
}
getfullName() {
return `${this.firstname} ${this.secondname}`;
}
另一种方法是创建一个函数,该函数每次基于数组调用时都返回不同的名称
组成部分
模板
Get Name{{selectedName}
您不能在一个块中添加多个return
语句,相反,您可以返回具有这两个值的对象,并在任何需要的地方使用它!