我想这必须是一个基本问题,但是我一直在努力解决这个问题太久了.我对流星来说比较新.
我查看了Meteor.user()(http://docs.meteor.com/#meteor_users)的文档,可以看到如何向user.profile添加其他信息.也就是说,
//JS file
Meteor.users.insert({
username: 'admin',
profile: {
first_name: 'Clark',
last_name: 'Kent'
},
});
Run Code Online (Sandbox Code Playgroud)
那么如何在视图模板中显示配置文件信息?我可以通过视图和Web控制台(Meteor.user())访问用户对象,但是我无法访问对象详细信息.
我最初的想法是,我可以在我的车把模板中加载以下内容,但它们不起作用:
// HTML view
{{Meteor.user().username}}
{{Meteor.user().profile.first_name}}
{{Meteor.user().profile.last_name}}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
cha*_*hne 28
你的插入是正确的.
但要显示像名字一样的信息,你必须提供帮助函数.
你的html模板:
<template name="user">
<p>{{firstName}}</p>
</template>
Run Code Online (Sandbox Code Playgroud)
你的js代码:
Template.user.helpers({
firstName: function() {
return Meteor.user().profile.first_name;
}
});
Run Code Online (Sandbox Code Playgroud)
您还可以使用{{currentUser}}帮助程序包装用户模板,以确保有用户.
{{#if currentUser}}
{{> user}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)
kon*_*tur 15
如果您不想为嵌套的对象的不同属性定义代理帮助器{{currentUser}},则可以在模板中完全执行以下操作:
{{#with currentUser}}
{{#with profile}}
{{first_name}}
{{/with}}
{{/with}}
Run Code Online (Sandbox Code Playgroud)
更新以反映评论建议.
| 归档时间: |
|
| 查看次数: |
26252 次 |
| 最近记录: |