【问题标题】:Why Am I Unable to Pass Data to Iron-Router Route Template?为什么我无法将数据传递到 Iron-Router 路由模板?
【发布时间】:2014-12-28 21:59:21
【问题描述】:

我刚刚更新到 Meteor 1.0 和最新版本的 Iron-Router。

无论出于何种原因,当我将数据传递给仅包含单个文档的模板时,它都能正常工作。但是当我尝试将多个文档传递给模板时,我得到一个空白屏幕。

//galleryRoute.js

Router.route('/:section', function() {
    this.layout('Gallery', {
        data: function() { 
            var data =  { photos: photos.find({ path: { $regex: '/'+this.params.section +'/' } }) };
            return data;
        }
    });
});

<template name="Gallery">
    <div class="container">
        {{#each photos}}    
            <div class="section-photo" style="background:url({{path}}) no-repeat center center; width:175px; height:175px; background-size:cover;"></div>
        {{/each}}
    </div>
</template>

想知道是否有人对为什么会这样有任何想法?

【问题讨论】:

标签: javascript meteor iron-router spacebars


【解决方案1】:

在您的数据函数中,您正在访问this.params.section。但是,this 是指您当前的范围,而不是路由,因此 this.params 将是未定义的。尝试做这样的事情:

//galleryRoute.js

Router.route('/:section', function() {
    var route = this;
    this.layout('Gallery', {
        data: function() { 
            var data =  { photos: photos.find({ path: { $regex: '/'+route.params.section +'/' } }) };
            return data;
        }
    });
});

【讨论】:

  • Iron Router 文档明确指出您应该通过在this section 中引用this.params 来访问路由参数。
  • 您的示例不同,因为您试图在新范围内访问thisparams 属性,即分配给data 属性的lambda 函数的范围。在该函数之外,在父范围内,this.params 可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2013-09-10
  • 2015-02-12
  • 2015-06-05
  • 2015-04-11
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多