这是我的代码:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <h1>Hello {{name}}</h1>
  <p><strong>Email:</strong> {{email}}</p>
  <p><strong>Address:</strong> {{address.street}}, {{address.city}}, {{address.country}}.</p>
  `,
})
export class AppComponent  {
  name = 'Strahinja';
  email = 'strahinja@gmail.com';
  address = {
    street: 'Boulevard of king Alexander',
    city: 'Belgrade',
    country: 'Serbia'
  }
  addressToString(): string {
    return this.address.street + ', ' + this.address.city + ', ' this.address.country;
  }
}


我想接下来打印出来:Address: Boulevard of king Alexander, Belgrade, Serbia
我收到的输出应为Address: [object Object]。我知道有一种方法可以通过编写next来打印我想要的所有数据:
Address: {{address.street}}, {{address.city}}, {{address.country}}
而且我知道有一种创建toString()方法的方法,因为Java中有一种方法,我尝试通过浏览Web来做到这一点,但是我在最后一行的最后一个this上遇到了编译错误,它说下一步:';' expected. Unreachable code detected.
有什么建议吗,伙计们?是否有可能在打字稿的类中仅获得2个属性?

最佳答案

尝试改变

return this.address.street + ', ' + this.address.city + ', ' this.address.country;




return this.address.street + ', ' + this.address.city + ', ' + this.address.country;


您在','和this.address.country之间缺少'+'

10-05 20:51
查看更多