我想使用 Meteor 将一些数据从模板共享到另一个模板。我有一个模板,即 allInventory.html,我在上面以表格形式显示一些数据,我在那里添加了三个链接。一个用于查看、编辑和删除我想要的内容我将所有数据从后端获取到助手之一,即 productDetails,我将一个事件与查看按钮绑定(bind),该事件将获取当前用户单击哪个产品的数据,因此我已成功获取数据在我的 allinventory 模板中,但还有另一个模板,即我想在其上呈现或显示该数据的 productDetails。但坚持我有关于 allInventory 点击事件的数据,但不知道如何与 productDetails 模板共享相同的数据。
这是我的 allInventory.js
Template.allInventory.rendered = function() {
Template.allInventory.events({
"click .btn":function (e){
data = $(e.target).attr('data');
Router.go('productDetail', {data: $(e.target).attr('data')}, {query: 'q=s', hash: 'hashFrag'});
console.log("button clicked.."+data);
console.log(data);
}
})
ProductDetails.js
Template.productDetail.rendered = function () {
Template.productDetail.helpers({
productDetails: function() {
return data;
}
});
allInventory.html
<button type="button" data ="{{productInfo}}" class="btn btn-info btn-sm"><i class="fa fa-eye"></i>View</button>
我只是想与 productsDetails 模板共享 allInventory 模板数据。
如有任何帮助,我们将不胜感激! 谢谢
最佳答案
为此,我建议避免使用 Session,因为它是一个全局对象,但更重要的是,因为有更好的方法可以做到这一点。
您可以使用助手将数据从父模板传递到子模板:https://guide.meteor.com/blaze.html#passing-template-content
您可以使用回调将数据从子模板传递到父模板https://guide.meteor.com/blaze.html#pass-callbacks
我将此应用构建为具有容器(页面)模板,该模板将包含所有订阅并根据 URL 呈现您的模板之一。
关于node.js - 如何将一个模板的一些数据发送到另一个模板 Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37937964/