![](/img/trans.png)
[英]How can I fix variables of an array of objects being undefined when being imported from a json file?
[英]How to fix FlowRouter.getParam from being 'undefined'
我正在向網站添加一個新頁面,我正在復制已經存在的代碼,目前正在該網站上工作。 為什么FlowRouter.getParam在其他地方工作時未定義?
客戶機/ JobInvoice.js
import { Invoices } from '../../../imports/api/Invoice/Invoice';
Template.InvoicePage.onCreated(function(){
const user = FlowRouter.getParam('_id');
console.log(user);
this.subscribe('invoices', user);
});
LIB / router.js
Accounts.onLogout(function(){
FlowRouter.go('home');
});
FlowRouter.notFound = {
action: function() {
FlowRouter.go('/404');
}
};
const loggedIn = FlowRouter.group({
prefix: '/secure'
});
loggedIn.route( '/invoice', {
name: 'invoice',
action() {
BlazeLayout.render('FullWithHeader', {main:
'InvoicePage'});
}
});
我錯過了什么?
FlowRouter允許您定義具有動態屬性( path-to-regexp )的路由,這些屬性通常表示文檔ID或其他動態屬性。
例如
FlowRouter.route('/invoice/:docId', { ... })
將定義一個匹配/invoice/9a23bf3uiui3big
等模式的路由,您通常使用它來為單個文檔渲染模板。
現在,如果您想在相應的模板中訪問文檔ID作為param docId
,您將使用FlowRouter.getParam('docId')
,它將返回上述路由9a23bf3uiui3big
。
由於您的路由定義缺少動態屬性,因此FlowRouter.getParam
不會接收任何參數。
可能的解決辦法是
loggedIn.route( '/invoice/:_id', {
name: 'invoice',
action() {
BlazeLayout.render('FullWithHeader', {main:
'InvoicePage'});
}
});
以與其他模板相同的方式訪問它。
閱讀
https://github.com/kadirahq/flow-router#flowroutergetparamparamname
這是我最終做的,它的工作原理。
loggedIn.route( '/invoice/:id', {
name: 'invoice',
action() {
BlazeLayout.render('FullWithHeader', {main: 'InvoicePage'});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.