Meteor Spacebars {{#if someCondition}} shows data briefly on page refresh

前端 未结 3 1156
独厮守ぢ
独厮守ぢ 2021-01-24 18:16

I have tried this a couple different ways and they both behave the same way (see below for code). I\'m using a spacebars if condition (and tried using a helper as well) to check

3条回答
  •  孤街浪徒
    2021-01-24 18:45

    You can use instead, the currentUser helper from accounts package, like this.

    {{#if currentUser}}
    
    {{else}}
     {{> login }} 
    {{/if}}
    

    Iron Router Option.

    There is also a solution in the router level, using Router.onBeforeAction.

    // lib/routes.js
    // Create the function to check login.
       var requireLogin = function() {
           if (! Meteor.user()) {
              this.render('login');
            } else {
              this.next(); //using this.next the iron router will render the route named on the onBefore
          }
       }
    
       Router.onBeforeAction(requireLogin, {only: 'theRoute});
    

    UPDATE

    Template.headerTpl.helpers({
      isLogged:function(){
       if(Meteor.user()){
          return true;
        }else{
          return false;
        }
      }
    })
    
    
    

提交回复
热议问题