簡體   English   中英

顯示Meteor.user()

[英]Displaying Meteor.user()

我有這個模板:

<template name="index">
  {{#if currentUser}
    {{userid}}
  {{/if}}
</template>

之間有什么區別

Template.index.helpers({
    userid: function() {
        return Meteor.user()._id;
    }
});

Template.index.helpers({
    userid: Meteor.user()._id
});

最后一個給出此錯誤: Uncaught TypeError:無法讀取未定義的屬性'_id'

最簡單的方法是使用{{currentUser._id}}

Meteor.user()._id是正確的。 問題是當您加載頁面時,最初數據不可用, Meteor.user()將為空,直到建立DDP連接並且瀏覽器將您登錄為止(這需要幾百毫秒)。 Meteor.user() && Meteor.user()._id來更正此問題。

這兩個Meteor.user()._id之間的區別在於,您傳遞的是在加載模板時加載的第一個靜態值。 即使Meteor.user()更改或您注銷,它也將堅持下去。

傳遞function您告訴助手可以重新計算,並且如果有任何反應式更改,它將更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM