【问题标题】:Router for static site using Meteor使用 Meteor 的静态站点路由器
【发布时间】:2015-01-01 06:12:13
【问题描述】:

我是 Meteor 的新手,对 javascript 的经验有限。我已经搜索了所有堆栈帖子和网络,以获取有关如何使用 Iron Router 包路由静态页面的好教程,但似乎无法弄清楚这一点。我希望有人帮助我了解如何为 Home 和 About 设置 router.js 文件。我经常使用我的代码,但这是我在此处发布之前所拥有的。从概念上讲,我似乎很难掌握路由的工作原理以及铁路由的所有各种功能,然后将这些路由连接到一个导航栏,我可以在它们之间导航。提前感谢您提供的任何帮助。

客户端/模板/应用程序/layout.html

<template name="layout">
  <div class="container">
    {{> yield}}
  </div>
</template>

lib/router.js

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

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

  this.render('about', {
    template: 'about'
  });
});

模板/home.html

<template name="home">
  <div class="container">
    <h2>Home</h2>
  </div>
</template>

【问题讨论】:

标签: meteor iron-router


【解决方案1】:

上面的代码看起来是正确的。

一个怪癖是你为你的/ 路由渲染了两个页面。你应该有这个:

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

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

记住this.render将第一个参数作为模板,所以不需要再单独定义了。

还有一个新的 about.html 页面:

<template name="home">
    <div class="container">
        <h2>Home</h2>
    </div>
</template>

现在你可以使用//about 页面(至少我希望我没有错过任何东西)

【讨论】:

    【解决方案2】:

    您的文件夹中可以有 3 个模板

    客户/视图

    同名

    about.html
    main.html
    admin.html
    layout.html
    (for example)
    

    所以在 about.html 你有这个

    <template name="about">
    <h1> hello from about page
    </template>
    
    <template name="main">
    <h1> hello from about page
    </template>
    
    <template name="admin">
    <h1> hello from about page
    </template>
    

    Layout.html 文件需要包含渲染的这个产量。

    <template name="layout">
      {{> yield}}
      {{admin}}
      {{about}}
      {{main}}
    </template>
    

    所以你可以将布局模板用作母版页并调用由路由分隔的 3 个模板,如何分配路由并告诉流星使用该布局,请使用此 js 代码

    JS

    Router.configure({
      layoutTemplate: 'layout'
    });
    Router.map(function(){
      this.route('admin', {path: '/admin'});
    });
    Router.map(function(){
      this.route('about', {path: '/about'});
    });
    Router.map(function(){
      this.route('main', {path: '/'});
    });
    

    至少这对我有用,兄弟,希望这对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2015-07-19
      • 2019-11-11
      • 2017-10-03
      • 2016-06-03
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多