我有一个使用铁路由器的Meteor项目。
在我的项目中,我有一个自举导航栏,不幸的是,每当我在导航栏扩展时导航到路线时(由于屏幕为最小宽度),当我转到新路线时,导航栏仍会展开。
有没有办法在更改路线时将导航栏设置为再次折叠,就像通常的页面刷新一样?
修改
导航栏模板:
<template name="navigation">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{pathFor route='home'}}"><span class="glyphicon glyphicon-home"></span> RNG Leaderboard</a>
</div>
<div class="navbar-collapse collapse" id="navbar-collapse" aria-expanded="false" style="height: 1px;">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-phone"></span> Games <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="/headsortails">Heads or Tails</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-stats"></span> Leaderboards <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="/headsortailsleaderboard">Heads or Tails</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
{{#if currentUser}}
{{#if isInRole 'admin'}}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-cog"></span> Maintenance <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="/manageusers"><span class="glyphicon glyphicon-user"></span> Manage Users</a>
</li>
</ul>
</li>
{{/if}}
<li><a href="/@{{currentUser.username}}"><span class="glyphicon glyphicon-user"></span> {{currentUser.profile.firstName}} {{currentUser.profile.lastName}}</a></li>
<li><a id="logout" href="{{pathFor route='login'}}"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
{{else}}
<li><a href="{{pathFor route='login'}}"><span class="glyphicon glyphicon-log-in"></span> Log in</a></li>
<li><a href="{{pathFor route='signup'}}"><span class="glyphicon glyphicon-pencil"></span> Sign up</a></li>
{{/if}}
</ul>
</div>
</div>
</nav>
</template>
路由器代码:
Router.configure({
layoutTemplate: 'layout', // Defines the layout template
loadingTemplate: 'loading', // Defines the loading template
onAfterAction: function(){
$('.nav-collapse').collapse('hide');
}
});
Router.plugin('dataNotFound', {
notFoundTemplate: 'dataNotFound'
});
Router.route('/', {
name: 'home',
template: 'home'
});
Router.route('/signup');
Router.route('/login');
Router.route('/recoverpassword');
Router.route('/resetpassword');
Router.route('/manageusers', {
name: 'manageusers',
controller: 'ManageUsersController',
});
ManageUsersController = RouteController.extend({
template: 'manageusers',
before: function() {
if (!Roles.userIsInRole(Meteor.userId(), 'admin')) {
this.redirect('home');
} else {
this.render('manageusers');
}
}
});
var usernameRoute = '/@:username';
Router.route(usernameRoute, {
name: 'profile',
controller: 'ProfileController'
});
ProfileController = RouteController.extend({
template: 'profile',
waitOn: function() {
return Meteor.subscribe('userProfile', this.params.username);
},
data: function() {
var username = Router.current().params.username;
return Meteor.users.findOne({
username: username
});
}
});
Router.route('/headsortails');
Router.route('/headsortailsleaderboard');
答案 0 :(得分:1)
一个不优雅但实用的解决方案是检查导航栏是否展开然后点击它 - 根据Barry Doyle的解决方案
closeNavBar: function() {
var isExpanded = $('.navbar-toggle').attr('aria-expanded') === true;
if(isExpanded) {
$('.navbar-toggle').click();
}
this.next();
},
答案 1 :(得分:0)
根据Michel的回答和一些挖掘,我在router.js文件中提出了以下解决方案:
Router.configure({
onAfterAction: function(){
$('.navbar-toggle').click();
}
});
答案 2 :(得分:0)
Neigher都提供了解决方案。
必须是这样的。 attr()
返回字符串'true'
或'false'
不是布尔值,这就是使用=== 'true'
的原因。
Router.configure({
...
onAfterAction() {
const navbarToggle = $('.navbar-toggle');
if (navbarToggle.attr('aria-expanded') === 'true') {
navbarToggle.click();
}
}
});