簡體   English   中英

流星大火renderWithData

[英]Meteor Blaze renderWithData

我正在嘗試通過Blaze渲染模板。 所以通常我打電話

Blaze.render(Template.xxx,$("#parentnode")[0]);

效果很好。

現在,我要區分渲染它的模板xxx。 所以我想將一個變量傳遞給Template,我可以用它來確定它是誰。 我試過了

Blaze.renderWithData(Template.xxx,{test:"value"},$("#parentnode")[0]);

然后嘗試訪問中的數據

 Template.xxx.onRendered(function(){console.log(this.test)});

哪個記錄“ this.test is not blabla”。 如何正確傳遞和訪問數據?

感謝您的答復,祝您有一個愉快的答復!

根據流星文檔 ,不可能在onRendered回調中訪問上下文對象。 this關鍵字引用模板實例,不包含任何有狀態數據。 您可以做的是在DOM節點內渲染數據屬性(如果不想顯示,則在隱藏元素中)。 您可以從onRendered函數訪問DOM。

呈現模板時,Template.currentData()可用,因此請嘗試使用Template.currentData()代替它,它應該可以工作:

Template.xxx.rendered = function() {
  var _this = Template.currentData();
  console.log(_this.test)
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM