【问题标题】:Setting template data context with Router.go使用 Router.go 设置模板数据上下文
【发布时间】:2015-12-10 01:28:57
【问题描述】:

使用 Iron 路由器,我可以像这样设置模板的数据上下文:

Router.route('hello', {data: {greeting: 'hello'}})

如果我的hello.html 看起来像这样:

<template name="hello">
  <h1>{{greeting}}</h1>
</template>

正如预期的那样,我得到一个页面说你好

问题: 我可以用Router.go() 做到这一点吗? 根据我的阅读,这应该可行:

Router.go('hello', {greeting: 'Whazaaaaaaa'})

但我得到的只是一个无聊的你好。可以这样设置数据吗?如果 Router.go 中的第二个参数不是数据,那它是干什么用的?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    是的,应该可以。第二个参数作为参数传递给路由函数,因此您需要相应地设置它。试试这个:

    Router.route('/hello/:greeting', {
      template: 'hello',
      data: function() {
        return {greeting: this.params.greeting};
      }
    });
    

    现在你可以打电话了:

    Router.go('hello', {greeting: 'Whazaaaaaaa'})
    

    【讨论】:

    • 我需要在路由中使用 :_greeting 吗?
    • this.params 未定义
    • 尝试类似:Router.route('/hello/:_greeting', {data: {greeting: this.params._greeting?this.params.greeting:'hello'}});
    • 对不起,我更新了答案。代码现在可以工作了
    • 所以第二个参数不是data,只是设置url路由中使用的参数?
    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 2015-08-11
    • 2019-08-04
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多