首页 > 解决方案 > 如何在角度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
  }

标签: javascriptangularreturn-value

解决方案


您不能在单个块中添加多个返回语句,而是可以返回具有两个值的对象并在任何您想要的地方使用它,例如 -

getfullName() {
      return {firstName: this.firstname, lastName: this.secondname}
}

推荐阅读