Javascript 如何在Meteorjs的特定URL路径中隐藏模板上的按钮?
我对Meteorjs很陌生,但发现它很好用 我想知道根据路由/url隐藏模板元素的最佳方法是什么。因为我想在其他地方再次使用相同的模板,但其中没有某些元素 谢谢 我的代码根据下面的解决方案 我的助手Javascript 如何在Meteorjs的特定URL路径中隐藏模板上的按钮?,javascript,meteor,Javascript,Meteor,我对Meteorjs很陌生,但发现它很好用 我想知道根据路由/url隐藏模板元素的最佳方法是什么。因为我想在其他地方再次使用相同的模板,但其中没有某些元素 谢谢 我的代码根据下面的解决方案 我的助手 Template.postItem.helpers({ ownPost: function() { return this.userId === Meteor.userId(); }, commentsCount: function() { return Commen
Template.postItem.helpers({
ownPost: function() {
return this.userId === Meteor.userId();
},
commentsCount: function() {
return Comments.find({postId: this._id}).count();
},
routeNameEqual: function(name){
var routeName= Router.current().route.getName();
return routeName === name;
}
});
我的html
{{#if routeNameEqual 'postsList2' }}
<a href="{{pathFor 'postPage'}}" class="discuss btn btn-default">Learn More</a>
{{else}}
{{/if}}
{{#如果routeNameEqual'postsList2'}
{{else}
{{/if}
关键!!它是路由名称,而不是路由路径如果您正在使用,则可以使用
if(Router.current().location.get().path == 'some check'){
$('#button').hide();//use jQuery
}else{
$('#show').hide();//use jQuery
}
同时检查
您还可以标记javascript,因此这里是另一个使用javascript的解决方案
if(document.URL == 'some check'){ // or use window.location.href
$('#button').hide();//use jQuery
}else{
$('#show').hide();//use jQuery
}
如果解析路径看起来很乏味,您也可以使用路由名称(同样,假设iron:router) 如果要避免操作DOM,或者首先要避免在模板中包含内容,那么请使用模板帮助器 HTML:
请注意,在此帮助器需要一个“name”参数,即路由名称。在HTML中,我们查看路由名称是否为“bar”。这使您可以灵活地将其用于任何路由。这会返回模板名称还是路由名称路由名称。在我的开发中,我通常使用模板名==路由名
Router.current().route.getName()
<template name="foo">
{{#if routeNameEqual 'bar'}}
.. content
{{/else}}
.. alternate content
{{/if}}
</template>
Template.foo.helpers({
routeNameEqual: function(name){
return Router.current().route.getName() === name;
}
});