【问题标题】:Meteor activating template based on localStorage基于localStorage的流星激活模板
【发布时间】:2016-06-08 02:22:30
【问题描述】:

我的 Meteor 模板是这样排列的:

<template name="main">
{{#if youwashere}}
{{> content}}
{{#else}}
{{> introduction}}
{{/else}}
{{/if}}
</template>

为了确定用户以前是否访问过我的网站,我设置了一个本地存储,它可以工作。然后我尝试这样称呼它:

Template.main.helpers({
youwashere(){ return localStorage.getItem('herebefore'); }
});

应该发生的是:如果有一个本地存储项('herebefore'),那么它将显示名为content 的模板。但如果您的计算机上没有该项目,它会显示模板introduction。帮助程序代码最终都没有显示,所以我想知道我做错了什么。

编辑:要存储在本地存储上,我使用以下内容:

Template.introduction.events({
'click #button':function(){
localStorage.setItem('herebefore', true);}
});

【问题讨论】:

  • 你在 localStorage 中存储了什么值?当getItem返回时,它应该评估为“真实”

标签: javascript meteor meteor-blaze


【解决方案1】:

else 的语法有点不对劲:

<template name="main">
{{#if youwashere}}
  {{> content}}
{{else}}
  {{> introduction}}
{{/if}}
</template>

【讨论】:

    【解决方案2】:

    我能够像这样解决它。无论出于何种原因,else 语法都无法按我希望的方式工作。

    <template name="main">
    {{#if beenhere}}
    {{> currentevents}}
    {{/if}}
    {{#unless beenhere}}
    {{> introduction}}
    {{/unless}}
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2014-06-09
      • 2015-01-10
      • 2015-05-07
      • 2012-06-26
      • 2019-01-11
      相关资源
      最近更新 更多