Meteor Dynamic Template not working

2019-08-03 02:06发布

问题:

The line {{> Template.dynamic template=content }} makes my page not load. It actually crashes my browser sometimes. I had it working for a while but something happened and now it does not work anymore. {{> Template.dynamic template='navBar' }} works so my packages should be ok.

Meteor: 1.4 Packages: kadira:flow-router, kadira:blaze-layout

imports/ui/layouts/mainLayout.html:

<template name="mainLayout">
  <header>
    <div class="container">
      {{> navBar }}
    </div>
  </header>
  <body>
    <div class="container">
      {{> Template.dynamic template=content }} <!-- not working -->
    </div>
  </body>
  <footer>
    <div class="container">
      <h3>Footer</h3>
    </div>
  </footer>
</template>

imports/ui/layouts/mainLayout.js:

import { Template } from 'meteor/templating';
import './mainLayout.html';
import '../components/navBar.html';
import '../pages/settings.html';

imports/startup/client/routes.js:

import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import '../../ui/layouts/mainLayout.js';
import '../../ui/pages/settings.js';

FlowRouter.route('/', {
  action() {
    BlazeLayout.render('mainLayout', { content: 'mainLayout' });
  },
});

FlowRouter.route('/settings', {
  action() {
    BlazeLayout.render('mainLayout', { content: 'settings' });
  },
});

imports/ui/pages/settings.html:

<template name="settings">
  <div class="container">
    <h1>This is the settings page</h1>
  </div>
</template>

回答1:

This route:

FlowRouter.route('/', {
  action() {
    BlazeLayout.render('mainLayout', { content: 'mainLayout' });
  },
});

is not going to work because you are inserting the mainLayout component into itself - the nested content helper is the issue. Instead, you should be rendering a different component into content:

BlazeLayout.render('mainLayout', { content: 'home' }); // or whatever component should be at "/"