【问题标题】:Uncaught Error: No Iron.Layout found so you can't use yield未捕获的错误:未找到 Iron.Layout,因此您无法使用 yield
【发布时间】:2015-11-10 19:03:21
【问题描述】:

使用 Meteor 0.9.3 和 iron:router 1.0.0-pre2,即使安装了 iron:layout,此错误也会显示在控制台上,见下文:

willems-mini:iron willem$ meteor add iron:router@=1.0.0-pre2
added iron:location at version 1.0.0-pre2
added iron:dynamic-template at version 1.0.0-pre2
added iron:router at version 1.0.0-pre2
added iron:layout at version 1.0.0-pre2
added iron:middleware-stack at version 1.0.0-pre2
added iron:url at version 1.0.0-pre2
added iron:controller at version 1.0.0-pre2
added iron:core at version 1.0.0-pre2

iron:router: Routing specifically designed for Meteor

没有其他包,只有meteor的默认包:

willems-mini:iron willem$ meteor list
autopublish      1.0.0  Publish the entire database to all clients
insecure         1.0.0  Allow all database writes by default
iron:router      1.0.0-pre2  Routing specifically designed for Meteor
meteor-platform  1.1.1  Include a standard set of Meteor packages in your app

我正在尝试运行一个非常简单的应用程序:

1 个 javascript 文件:

Router.route('/', function () {
    this.render('home');
});

if (Meteor.isClient) {
    Template.home.events({
        'click button': function () {
            console.log('click!');
        }
    });
}

和 1 个 html 文件:

<head>
    <title>iron router test</title>
</head>

<body>
    {{> defaultLayout}}
</body>


<template name="defaultLayout">
    <header>
        {{> yield "header"}}
    </header>

    <article>
        {{> yield}}
    </article>

    <footer>
        {{> yield "footer"}}
    </footer>
</template>


<template name="home">
    {{#contentFor "header"}}
        <button>click header</button>
    {{/contentFor}}

    <button>click</button>

    {{#contentFor "footer"}}
        <button>click footer</button>
    {{/contentFor}}
</template>

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    iron:router 布局不应该这样工作。

    摆脱你在正文中明确包含布局:

    {{! this is WRONG, remove the body tag altogether }}
    <body>
        {{> defaultLayout}}
    </body>
    

    您指定layoutTemplate 的地方在RouteController 中:

    Router.route('/', function () {
      this.render('home');
    },{
     layoutTemplate:"defaultLayout" 
    });
    

    明确声明您的RouteControllers 通常是一种更好的设计模式。

    lib/router.js

    Router.route("/",{
      // give the route a name so it figures out itself to use :
      // - HomeController
      // - a template name "home"
      name:"home"
    });
    

    lib/controllers/lib/default-layout.js

    DefaultLayoutController=RouteController.extend({
      layoutTemplate:"defaultLayout"
    });
    

    lib/controllers/home.js

    HomeController=DefaultLayoutController.extend({
      //
    });
    

    【讨论】:

    • 这解决了问题(谢谢),但是,它给我留下了另一个问题:我有点惊讶地发现来自命名收益区域(“页眉”和“页脚”)的点击) 不会作为“home”模板的事件而结束。我只能在“defaultLayout”模板的事件处理程序中看到这些点击事件。有没有办法在“home”模板事件处理程序中注意到这些事件?
    • 刚刚找到了一种方法:我使用新的 iron-router 功能在路由控制器上定义事件处理程序。这样我就可以在一个地方获得所有“家庭”模板事件。
    【解决方案2】:

    嗯,就像你的错误说你错过了一个铁布局。

    在您的 lib/router.js 或您持有路由器代码的任何地方可能看起来像这样:

    Router.configure({
        layoutTemplate: 'layout',
        loadingTemplate: 'loading'
    });
    

    所以相应的&lt;template name="layout"&gt; 应该在那里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2023-02-11
      • 2018-01-01
      • 1970-01-01
      相关资源
      最近更新 更多