【问题标题】:Using Iron Router, "Router is not defined"使用 Iron Router,“未定义路由器”
【发布时间】:2014-02-13 06:53:26
【问题描述】:

我在我的应用程序中添加了 Iron-router 来处理主页、关于页面和应用程序主页(即地图)之间的路由

用陨石添加铁路由器后, 我编写了一个 router.js 文件并将其放在我的 /client 文件夹中,但是我收到一条错误消息“未捕获的 ReferenceError:未定义路由器”

我使用 chrome devtools 检查了错误,它指向了我在下面添加的 router.js 开头的“Router.configure(...”

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

Router.map( function () {
//the about route
this.route('about', {
    path: '/about',
    template: 'about',
    action: function () {
        console.log('now routing the about template');
    }
});

this.route('home', {
    path: '/',
    template: 'home',
    action: function () {
        console.log('now routing the home template');
    }
});

//the map route
this.route('map', {
    path: '/map',
    template: 'map',
    action: function () {
        console.log('now routing the map template');
    }

});
});

有谁知道为什么我收到路由器未定义的错误?

【问题讨论】:

  • 我将 router.js 文件移动到 /lib 目录,以便它首先加载。这破坏了应用程序,所以我然后卸载并重新安装了带有陨石的铁路由器,路由现在可以在我的应用程序中运行。我希望这可以帮助陷入类似情况的人。

标签: meteor iron-router


【解决方案1】:

确保您的流星版本是 0.8.3 或更高版本,并通过命令使用流星

mrt 添加铁路由器

其他会导致iron router搭建错误。

否则,请检查您的路由器配置代码是否在 Meteor.isClient 范围内。 如果没有,只需将它们包装在客户端范围内。

if(Meteor.isClient){
  Router.configure({
   ......
  });

  Router.map(function(){
   ......
  });
}

如果 Iron-Router 安装在 0.8.3 以下版本,您应该从包和 smart.json 中删除它们, 用命令更新流星,

流星更新

然后用陨石重新安装铁路由器。
如果没有出现错误信息,则一切正常

【讨论】:

    【解决方案2】:

    我在 Meteor 0.8.3 中添加 Iron-router 时遇到过类似的问题。

    这个食谱似乎对我有用。

    • 将iron router添加到smart.json(或新建项目):

    {"packages": { "iron-router": {"version":"0.8.2"} }}

    • 流星更新

    此时,iron-router 位于 packages 文件夹中,但可能未在 .meteor/packages 列表中列出。如果没有:

    • 流星添加铁路由器

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      我知道您已解决此问题,但出于文档目的:

      将 Iron Router 放在 lib/ 目录中将允许客户端和服务器访问代码并首先加载。

      What are the best practices for structuring a large Meteor app with many HTML template files?

      【讨论】:

        【解决方案4】:

        iron-router 可能已正确安装但未配置为使用。它只是发生在我身上,我用你的问题来更多地理解我的问题。

        实际上,我在packagesmart.json/lock 中有这个包,但它没有在文件.meteor/packages 中使用,并且在meteor 启动时没有加载。所以它找不到它。只需添加包的名称即可修复它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-21
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 2016-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多