【问题标题】:Routing not working for me in Meteor with the help of FlowRouter在 FlowRouter 的帮助下,路由在 Meteor 中对我不起作用
【发布时间】:2017-01-06 16:02:38
【问题描述】:

我正在尝试借助流路由器包来应用路由。但它并不像我预期的那样对我有用。我将分享我的代码,请让我知道我哪里出错了!提前致谢。

FlowRouter.route('/',{
     name:'home',
     action(){
           BlazeLayout.render("HomeLayout");
     }
});

FlowRouter.route('/test',{
     name:'test',
     action(){  
         BlazeLayout.render('MainLayout',{main:'Test'});
     }
});

当我转到 localhost:3000/test 时出现以下错误。

modules-runtime.js:36Uncaught Error: Cannot find module './main.html'

Exception in defer callback: Error: Expected template or null, found: undefined
at ._render (http://localhost:3000/packages/spacebars.js?hash=65db8b6a8e3fca189b416de702967b1cb83d57d5:61:13)
at doRender (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2027:25)
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1875:20
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3687:12)
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1873:29
at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2214:12)
at viewAutorun (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1872:18)
at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?hash=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:339:36)
at new Tracker.Computation (http://localhost:3000/packages/tracker.js?hash=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:229:10)
at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?hash=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:604:11)

请告诉我哪里错了?我需要对我的代码进行任何更改吗?

这是我的 index.html 文件代码:

<head>
  <title>intermediate-meteor</title>
</head>

这是我的 MainLayout.html:

<template name="MainLayout">
    <header>
        <h1>My Recipe Book</h1>
    </header>
    <main>
        {{>Template.dyanamic template=main}}
    </main>
</template>

<template name="Test">
    <h1>Test Template Rendering form Main Template...!!</h1>
</template>

这是我的 HomeLayout.html:

<template name="HomeLayout">
    <header>
        <h1>My Recipe Book</h1>
    </header>
    <main>
        <div class="billboard">
            <h2>Organize your meals</h2>
        </div>
    </main>
</template>

Package.jason 文件代码:

# Meteor packages used by this project, one per line.
# Check this file (and the other files in this directory) into your repository.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

meteor-base@1.0.4             # Packages every Meteor app needs to have
mobile-experience@1.0.4       # Packages for a great mobile UX
mongo@1.1.12                   # The database Meteor supports right now
blaze-html-templates@1.0.4    # Compile .html files into Meteor Blaze views
reactive-var@1.0.10            # Reactive variable for tracker
jquery@1.11.9                  # Helpful client-side library
tracker@1.1.0                 # Meteor's client-side reactive programming library

standard-minifier-css@1.2.0   # CSS minifier run for production mode
standard-minifier-js@1.2.0    # JS minifier run for production mode
es5-shim@4.6.14                # ECMAScript 5 compatibility for older browsers.
ecmascript@0.5.8              # Enable ECMAScript2015+ syntax in app code
shell-server@0.2.1            # Server-side component of the `meteor shell` command



kadira:flow-router
kadira:blaze-layout

请检查我的代码并告诉我哪里出错了?

【问题讨论】:

    标签: meteor meteor-blaze flow-router


    【解决方案1】:

    您似乎删除了默认的 client/main.html 文件。您是否更新了 client/main.js 中的“import './main.html'”行?

    【讨论】:

    • 我只是把文件名从mian.html改成index.html。
    • 我也在学习Meteor,没有看FlowRouter的文档。如果将文件名从 main.html 更改为 index.html?你在 client/main.js 里面做了同样的事情吗?
    • No .. 我正在学习流星 js 表单教程,我遵循升级中级教程来学习流星 js。在该教程中,如果您可以看到他创建了两个布局,例如“MainLayout.html”和“HomeLayout.html”,然后他在 MainLayout、html 中创建了测试模板,并使用 flowrouter 他访问了该测试模板,但对我来说并非如此。我将分享您可以看到的链接,然后让我知道它是如何实现的。请分享您的想法谢谢。这是链接:- youtube.com/…
    • 如果你把main.html改成了index.html,你应该在client/main.js文件里面做相应的修改。试一试,看看它是否能消除该错误。
    • 好吧,我也试试这个,但这也不能按照我想要的方式工作。嘿,如果你也学习流星,如果你尝试过在流星中路由,请与我分享你的代码,它可能会以某种方式帮助我解决我的问题。如果您使用任何其他包,我正在使用 FlowRouter 包进行路由,请让我知道并分享您的代码,这样它将帮助我了解我想在流星中学习路由。谢谢..!
    【解决方案2】:

    您的index.html 文件是需要动态模板的文件:index.html

    <head>
      <title>intermediate-meteor</title>
    </head>
    <body>
      {{> layout}}
    </body>
    
    <template name="layout">
       {{> Template.dynamic template=main}}
    </template>
    

    然后您可以控制从路由中的哪个 URL 加载哪个模板。


    所以最后,你的路线应该是这样的:
    FlowRouter.route('/', {
      action: function() {
        BlazeLayout.render('layout', { main: 'MainLayout' });
      }
    });
    
    FlowRouter.route('/test', {
      action: function() {
        BlazeLayout.render('layout', { main: 'Test' });
      }
    });
    
    FlowRouter.route('/home', {
      action: function() {
        BlazeLayout.render('layout', { main: 'HomeLayout' });
      }
    });
    

    我的 HTML 设置带有一个仅加载主模板的 index.html 文件:

    <body>
      {{> layout}}
    </body>
    
    <template name="layout">
       {{> Template.dynamic template=main}}
    </template>
    

    然后我为每个模板创建一个新的 HTML 文件:test.html

    <template name="test">
       <h1>Hello, this is a test page</h1>
    </template>
    

    当然,您的 homelayout 模板在另一个文件中:homelayout.html

    <template name="homelayout">
       <h1>Hello, this is the home page</h1>
    </template>
    

    【讨论】:

    • 我已经创建了 MainLayout.html,这是我的主要模板。然后我创建了 HomeLayout.html 并且我有 index.html 我在 MainLayout.html 中创建了一个名为“Test”的模板,我根据我正在使用的流路由在 route.js 中编写路由代码在我的项目中,但它不适合我想要的方式..!你可以分享你完成的代码??抱歉,我是 Meteor 的新手,所以我仍然不知道我哪里出错了..!!
    • 我也收到一个错误“install.js:101Uncaught Error: Cannot find module './main.html'” 我不知道为什么?我的错在哪里?
    • 您是否同时安装了kadira:flow-routerkadira:blaze-layout?也许将您的 HTML 文件添加到您的帖子中,以便我们可以看到它们。
    • 是的,我已经安装了来自大气js .com 的两个包,它也显示在 package..json 文件中,但我不知道为什么它不能正常工作..!!
    • 你能把html文件加到帖子里吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2023-04-01
    • 2021-11-30
    • 1970-01-01
    • 2018-10-08
    相关资源
    最近更新 更多