这是我的代码:
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]
。我知道有一种方法可以通过下一步写下我想要的所有数据:
Address: {{address.street}}, {{address.city}}, {{address.country}}
。
我知道有一种方法可以创建一个toString()
方法,因为在Java中有一种方法,我尝试通过一些探索网络来实现它,但是我在最后一行this
上得到了一个编译错误然后它说:';' expected. Unreachable code detected.
伙计们,有什么建议吗?是否有可能只能在打字稿中的类中获得2个属性?
答案 0 :(得分:2)
尝试更改
.dot {
border-bottom: dotted 2px #022169;
}
<div class="dot"></div>
到
return this.address.street + ', ' + this.address.city + ', ' this.address.country;
你错过了','和this.address.country
之间的'+'答案 1 :(得分:2)
我在这里看到错字吗?我认为您在this.address.country
return this.address.street + ', ' + this.address.city + ', ' this.address.country;