我在一個網站,我在DB搜索字符串/而來後,在根域工作。我在文檔中找不到任何關於它的信息。 我想使它與Iron Router一起工作,但任何其他建議都可以解決。如何獲得在domain.com/keyword的「關鍵字」與鐵路由器
感謝您的幫助!
編輯:基本上我只是想將domain.com/後的任何內容傳遞給變量。
我在一個網站,我在DB搜索字符串/而來後,在根域工作。我在文檔中找不到任何關於它的信息。 我想使它與Iron Router一起工作,但任何其他建議都可以解決。如何獲得在domain.com/keyword的「關鍵字」與鐵路由器
感謝您的幫助!
編輯:基本上我只是想將domain.com/後的任何內容傳遞給變量。
這裏的東西我一直在做,所以也許它會導致你在正確的道路
路線發送URL params
到ownedGroupList
模板
Router.route('/users/:_id/groups', {
name: 'owned.group.list',
template: 'ownedGroupList',
data: function() {
return {params: this.params};
}
});
模板ownedGroupList
可以使用this.data
訪問params
對象onCreated
,onRendered
和onDestroyed
模板事件處理程序
Template.ownedGroupList.onCreated(function(){
this.subscribe("owned-groups", this.data.params._id);
});
模板ownedGroupList
可以在輔助方法訪問params
通過this
可變
Template.ownedGroupList.helpers({
groups: function() {
return Groups.find({owner: this.params._id });
}
});
模板ownedGroupList
可以在事件處理程序訪問params
通過template.data
可變
Template.ownedGroupList.events({
'click .a-button': function(event, template) {
var group = Groups.findOne({owner: template.data.params._id });
// do something with group
}
});
這裏有一個簡單的路由應該做的伎倆
Router.route('/:keyword', {
name: 'keyword',
template: 'keywordTemplate',
data: function() {
return this.params.keyword;
}
});
這會將關鍵字作爲數據上下文傳遞給您的模板,然後您可以隨心所欲地執行任何操作。或者,您可以直接在路由器中執行搜索(特別是如果您將關鍵字傳遞給訂閱以便搜索在服務器上運行)。例如:
Router.route('/:keyword', {
name: 'keyword',
template: 'keywordTemplate',
waitOn: function(){
return Meteor.subscribe('keywordSearch',keyword);
},
data: function() {
return MyCollection.find();
}
});
這第二個模式將您的關鍵字發送到一個名爲keywordSearch
訂閱,將在服務器上執行。當訂閱準備就緒時,路由的data
函數將運行,並且傳遞到您的keywordTemplate
的數據上下文將是MyCollection
中提供的任何文檔和字段。
'route(/:keyword',handler)'有什麼問題? –
感謝您的回答!我如何將'關鍵字'傳遞給一個全局變量,這樣我就可以在我的項目中使用它了? – Mark
編輯並感謝。 – Mark