How to use yield and Iron-router?

2020-05-08 04:15发布

问题:

So I am trying to do a basic meteor app at the moment.. here are the three files.

router.js:

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

Router.route("/", {
    name : "homeIndex",
    data : function() {
        return {
            message : "Welcome to the Rocket Shop"
        }
    }
});

index.html:

<template name="homeIndex">
    <h1>{{message}}</h1>
</template>

layout.html:

<template name="layout">
        {{> nav}}
        <div class="container">
            {{>yield}}
        </div><!-- /.container -->
</template>

When I go to localhost:3000 after starting the app I get a long stacktrace in my browser console and the body of the page does not load up.

22:35:57.444 Exception in callback of async function: MiddlewareStack.prototype.concat@http://localhost:3000/packages/iron_middleware-stack.js?ff70621b6c5f6a406edc60600c4b76126dae21d6:303:7
RouteController.prototype._runRoute@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:542:11
Route.prototype.dispatch@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:856:10
Route/route@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:713:5
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?ff70621b6c5f6a406edc60600c4b76126dae21d6:425:16
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?5deab0885176b44ccbbbf0b5101b065971c8f283:999:17
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?ff70621b6c5f6a406edc60600c4b76126dae21d6:449:3
RouteController.prototype.dispatch/</<@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:393:7
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:349:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:237:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:588:11
RouteController.prototype.dispatch/<@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:391:5
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:615:12
RouteController.prototype.dispatch@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:390:3
Router.prototype.dispatch@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:1700:3
onLocationChange@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:1784:20
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:349:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:237:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?9309a5697855cef52b32921fbc9dcb1017c58b39:588:11
Router.prototype.start@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:1777:31
Router/</<@http://localhost:3000/packages/iron_router.js?dd5fa02859b6335661b94134bd9903be8eecf44d:980:9
.withValue@http://localhost:3000/packages/meteor.js?5deab0885176b44ccbbbf0b5101b065971c8f283:971:17
withoutInvocation/<@http://localhost:3000/packages/meteor.js?5deab0885176b44ccbbbf0b5101b065971c8f283:428:26
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?5deab0885176b44ccbbbf0b5101b065971c8f283:999:17
onGlobalMessage@http://localhost:3000/packages/meteor.js?5deab0885176b44ccbbbf0b5101b065971c8f283:365:11
1 meteor.js:880:11

Does anyone possibly know what I am doing wrong? I am just following a pluralsight tutorial and have followed all of the directions.

回答1:

I figured out the answer to my own question after searching around stackoverflow at other iron-router questions.. I have no idea why it worked, but it solved the issue. All I had to do was run:

meteor add ejson

and I no longer got my error.

Here is the question that answered my question (which was posted AFTER my question): Iron:router 1.0.9 Not working



回答2:

The exception isn't very relevant, but I see two possibilities:

  • You forgot the template attribute in your Route.route();
  • Your router configuration file isn't in the right folder

For the first one:

Router.route("/", {
    name : "homeIndex",
    template : "homeIndex",
    data : function() {
        return {
            message : "Welcome to the Rocket Shop"
        }
    }
});

For the second one: make sure your router.js file is in client/lib/ folder.

Also, make sure that you have the iron-router package in the .meteor/packages.js file.