【问题标题】:Meteor Iron Router not loading templateMeteor Iron Router 未加载模板
【发布时间】:2015-08-31 08:20:05
【问题描述】:

我正在尝试使用 Iron 路由器加载模板,但没有加载任何用于路由的模板。单击项目时,url会改变,但当前页面永远不会改变。

Lib/router.js -

Router.configure({
  // we use the  appBody template to define the layout for the entire app
  layoutTemplate: 'appBody',

  // the appNotFound template is used for unknown routes and missing lists
  notFoundTemplate: 'appNotFound',

  // show the appLoading template whilst the subscriptions below load their data
  loadingTemplate: 'appLoading',

  // wait on the following subscriptions before rendering the page to ensure
  // the data it's expecting is present
  waitOn: function() {
    return [
      Meteor.subscribe('users'),
      Meteor.subscribe('roles')
    ];
  }
});

dataReadyHold = null;

if (Meteor.isClient) {
  // Show the loading screen on desktop
  Router.onBeforeAction('loading', {except: ['join', 'signin']});
  Router.onBeforeAction('dataNotFound', {except: ['join', 'signin']});

}

Router.map(function () {
  Router.route('home', {
    path: '/',
    onBeforeAction: function() {
      this.next();
    },
    action: function() {
      this.render('home');
    }
  });
  Router.route('adminPanel', {
    path: 'adminPanel',
    onBeforeAction: function() {
      this.next();
    },
    action: function() {
      console.log("hit admin");
      this.render('adminPanel');
    }
  });

});

两个链接:

            <li>
                <a href="/"><i class="fa fa-home"> Home</i></a>
            </li>
            <li>
                <a href="/adminPanel"><i class="fa fa-users"> Administration Panel</i></a>
            </li>

“home”模板非常基础:

<template name="home">
<h1>Welcome </h1>
<h2>Site in development</h2>
</template>

“adminPanel”模板:

<template name="adminPanel">
  <h1><i class="fa fa-users"> Administration Panel</i></h1>
    {{> addUserModal}}
    {{> tabular id="userTable" table=TabTables.Users class="table table-striped table-bordered table-condensed"}}
    {{> addRoleModal}}
    {{> tabular id="userTable" table=TabTables.Roles class="table table-striped table-bordered table-condensed" }}
    {{> autoformModals}}
</template>

如果有帮助,这里是文件结构:

.
├── client
│   ├── head.html
│   ├── routers
│   └── templates
│       ├── adminPanel.css
│       ├── adminPanel.html
│       ├── adminPanel.js
│       ├── app-not-found.html
│       ├── app-not-found.import.less
│       ├── appBody.html
│       ├── home.html
│       ├── loading.html
│       └── loading.import.less
├── lib
│   └── router.js
├── public
│   └── images
└── server
    └── publish.js

【问题讨论】:

  • 我们可以看到模板本身吗?他们的名字正确吗?您也可以使用铁路由路径,例如href="{{pathFor 'home'}}" 是使用路径作为路由的推荐方式。
  • 控制台没有错误?
  • 控制台中没有显示错误。使用 `href="{{pathFor 'home'}}" 时,还需要 router.js 中的功能吗?
  • 没有。如果您的路线定义正确,应该没问题。
  • 我已将模板和文件结构添加到原始问题中。

标签: javascript meteor iron-router


【解决方案1】:

您无需调用Router.map 函数。它没有多大作用,你可以看到here

如果你只是调用this.next(),你也可以省略onBeforeAction钩子。

但我也没有发现您的代码有任何问题。以下是您可以尝试的一些方法:

  • 检查您的subscriptions 是否正常工作
  • 确保在布局中调用&gt; yield
  • 检查appLoading 模板中的错误

【讨论】:

  • 感谢您的帮助。这是几件事的结合。它正在按照我现在想要的方式工作。
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
相关资源
最近更新 更多