Как визуализацию частичного шаблона, когда определенное меню щелкнуто в METEOR JS
-
21-12-2019 - |
Вопрос
Я новичок в Meteor JS.Как я могу сделать конкретный шаблон к определенной части веб-приложения, когда меню нажал.Я использую железо-маршрутизатор и шаблон макета. Следующий макет работает нормально.Например: когда пользователь нажимает на меню «Главная»> «Статья» Мне нравится представлять шаблон статьи в Maincontent Region (адресная панель выглядит как / myApp / артикул).Также другой пункт меню работает так же, когда пункт меню нажал, определенные шаблоны отображаются в Maincontent Part.Как я могу настроить это?Я не уверен, что даже возможно, есть ли другой путь или любое лучшее решение вокруг этой проблемы. Router.js
Router.map(function(){this.route('home', {
path: '/',
layoutTemplate: 'homePageLayout',
yieldTemplates: {
'myHeader': {to: 'header'},
'mySideMenu': {to: 'sideMenu'},
'myMainContent': {to: 'mainContent'},
'myFooter': {to: 'footer'}
}
.
}); });
layout.html
<template name="homePageLayout">
<div class="container">
<div class="row">
{{> yield region='header'}}
</div>
<div class="row">
<div class="col-lg-3">
{{> yield region='sideMenu'}}
</div>
<div class="col-lg-6">
{{> yield 'mainContent'}}
</div>
</div>
<div class="row">
<footer>
{{> yield region='footer'}}
</footer>
</div>
</div>
.
sidemenu.html
<template name="mySideMenu">
<div class="content"></div>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="glyphicon glyphicon-home"></span>
Home
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table">
<tbody>
<tr>
<td>
<a href="{{pathFor 'mission'}}"><span class="glyphicon glyphicon-pencil text-primary"></span>
Article
</a>
</td>
</tr>
<tr>
<td>
<a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
News
</a>
</td>
</tr>
<tr>
<td>
<a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
Report
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
Company
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
<table class="table">
<tbody>
<tr>
<td>
<a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
Mission
</a>
</td>
</tr>
<tr>
<td>
<a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
About us
</a>
</td>
</tr>
<tr>
<td>
<a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
Contact
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
.
Решение
Вы почти там :)
<div class="col-lg-6">
{{> yield 'mainContent'}}
</div>
.
Изменить на
<div class="col-lg-6">
{{> yield }}
</div>
.
Это «основной» выход.Железный маршрутизатор сделает шаблон маршрута здесь.Чтобы определить шаблон IR должен рендер, вы можете сделать 2 вещи.
ничего не делай -> Тогда железный маршрутизатор сделает шаблон с именем, равный имени маршрута.В вашем случае это сделает шаблон «домой».
или укажите шаблон для своего роутера:
Router.map(function(){
this.route('home', {
path: '/',
template: 'mySuperTemplateForThisRoute',
layoutTemplate: 'homePageLayout',
yieldTemplates: {
'myHeader': {to: 'header'},
'mySideMenu': {to: 'sideMenu'},
'myMainContent': {to: 'mainContent'},
'myFooter': {to: 'footer'}
}
});
});
. Другие советы
Я имею дело с моей проблемой, как это. вот мой маршрутизатор.js
Router.map(function(){
this.route('home', {
path: '/',
layoutTemplate: 'homePageLayout',
action: function(){
this.render('myHeader', {to: 'header'});
this.render('mySideMenu', {to: 'sideMenu'});
this.render('home', {to: 'mainContent'});
this.render('myFooter', {to: 'footer'});
}
});
this.route('showMission', {
path: 'mission',
layoutTemplate: 'homePageLayout',
action: function(){
this.render('myHeader', {to: 'header'});
this.render('mySideMenu', {to: 'sideMenu'});
this.render('mission', {to: 'mainContent'});
this.render('myFooter', {to: 'footer'});
}
});
this.route('showCompanyStructure', {
path: 'companyStructure',
layoutTemplate: 'homePageLayout',
action: function(){
this.render('myHeader', {to: 'header'});
this.render('mySideMenu', {to: 'sideMenu'});
this.render('companyStructure', {to: 'mainContent'});
this.render('myFooter', {to: 'footer'});
}
});
this.route('showDuties', {
path: 'duties',
layoutTemplate: 'homePageLayout',
action: function(){
this.render('myHeader', {to: 'header'});
this.render('mySideMenu', {to: 'sideMenu'});
this.render('duties', {to: 'mainContent'});
this.render('myFooter', {to: 'footer'});
}
});
});
.
Таким образом, когда нажата определенная ссылка меню, она отобразится, что шаблон на поле «Maincontent». Работает нормально.Но я думаю, что будет более хорошее решение, мое решение повторяет код, это может привести к тому, что «MyHeader» и «MyFooter» загружается более одного раза.Я не уверен в этом.