javascript - 如何在角度2中返回多个值?
问题描述
我想分别返回多个值,例如“firstname”和“secondname”。我怎样才能做到这一点?
我试图将它们作为字符串返回,但它不起作用:
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
}
解决方案
您不能在单个块中添加多个返回语句,而是可以返回具有两个值的对象并在任何您想要的地方使用它,例如 -
getfullName() {
return {firstName: this.firstname, lastName: this.secondname}
}
推荐阅读
- vue.js - 使用 Vuex,为什么 mapGetters 不接受与 mapState 相同的语法?
- docker - PACT.Net 无法分配请求的地址
- javascript - 滚动条显示在其他内容上
- python - 较新版本的 Cython Numpy 扩展编译失败
- java - 从文本文件中读取行,并通过将行中的值写入新的文本文件来按平均值(平均值)对它们进行排序
- sonarqube - 从 VSTS Build 机器在 SonarQube 上发布代码指标时出现错误“Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8”
- amazon-dynamodb - 用户表的 Dynamodb
- cordova-plugins - 在使用 Cordova 的 Android 中应用购买签名验证失败错误
- python - 使用python根据选择条件从json中提取值
- storefront - 如何在 Intershop7 中覆盖渲染 ISML 模板