流星-在发布模板之前检查登录状态

Meteor - Checking login status before rending templates

本文关键字:检查 登录 状态 布模板 流星      更新时间:2023-09-26

对流星不熟悉,并试图在呈现模板之前检查用户是否登录。我能够用下面的代码创建这个功能,但是它需要浏览器页面刷新,并且不会自动更新页面。

if (Meteor.isClient) {
 mustBeSignedIn();
}
function mustBeSignedIn() {
 if (Meteor.userId() == null) {
  console.log('Not logged in');
}
  else {
    loadTemplate_tableview()
  }
}

在不需要重新加载页面的情况下,最好的方法来解决这个问题,任何帮助将是感激的。

可以直接在模板中设置。

<template name="templateName">
    {{#if currentUser}}
        <p>You're logged in!</p>
    {{else}}
        <p>You're not logged in.</p>
    {{/if}}
</template>

有关currentUser帮助器的更多信息,请参见http://docs.meteor.com/#template_currentuser