我已经安装了一个 Meteor 电话认证包 mys:accounts-phone
,它应该在用户集合中添加一个 phone.number
子字段。我尝试按如下方式访问此字段:
Meteor.user().phone.number
但是 typescript 显示错误
Property 'phone' does not exist on type 'User'.
另一方面,我在 users.profile 中有自定义属性,可以通过这种方式轻松访问它们。 不安全尚未删除。自动发布已开启。
最佳答案
当我们的角度组件初始化但我们的 meteor 数据未从服务器到达时,有时会发生这种情况。 尝试使用用户注入(inject)代替 Meteor.user()
import {Component} from "@angular/core";
import { InjectUser } from 'angular2-meteor-accounts-ui';//<--**** import this****
@Component({
selector: "login-buttons",
template
})
@InjectUser('user') //<--*** add this***
export class LoginButtonsComponent {
user: Meteor.User; //<--*** add this ***
constructor(private router: Router) {}
}
现在在用户变量中你将拥有 Meteor.User 的所有值
如果你想在 html 部分打印,使用这个
<div *ngIf="user">
{{user.phone.number}}
</div>
不要忘记安装
meteor add accounts-password
meteor npm install --save angular2-meteor-accounts-ui
在 app.module.ts 文件中
import { AccountsModule } from 'angular2-meteor-accounts-ui';
@NgModule({
imports: [
... other modules here
AccountsModule
],
希望这会奏效。如果这不起作用让我知道我会告诉你另一种解决方案
关于meteor - 无法访问 Meteor.user() 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057276/