【问题标题】:Meteor links based on reactive var not working基于反应变量的流星链接不起作用
【发布时间】:2018-07-06 08:34:34
【问题描述】:

我尝试创建一些适应当前 url 的链接,所以当我当前的 url 是“object/layout1....”时,我的链接也应该是这样的,当 url 像“object/layout2”时,它应该是“object/layout2..”的链接。因此,我创建了这段代码,它应该将当前布局保存在一个反应​​变量中,并且应该在 url 更改时由于自动运行而刷新。这部分工作正常:

Template.templateName.onCreated(function(){
  //Save parameter layout in reactive var so it can be accessed by helper
  this.getParam = () => FlowRouter.getParam('parameter');

  this.autorun(() => {
    this.layout = new ReactiveVar(this.getParam());
  });
});

问题在于返回新链接的帮助程序没有更新。加载页面时,有正确的链接,但更改 url 后链接没有任何变化。这里是助手的代码:

'returnLink':function(postId) {
    return '/object/' + Template.instance().layout.get() + '?firstList=' + postId;
  }

我错过了什么,为什么我的布局中的链接没有更新? 我在布局中使用了助手,就像<a href="{{returnLink this._id}}">My new Link</a>

是的,我正在使用 flowrouter

【问题讨论】:

    标签: javascript html meteor meteor-blaze flow-router


    【解决方案1】:

    首先,如果您在 /object/layout1 上时希望 URL 为 /object/layout1/...,那么您可以使用相对 URL 并摆脱所有 onCreated() 代码,例如所以:

    'returnLink':function(postId) {
        return '?firstList=' + postId;
    }
    

    如果您仍然需要 flowrouter 中的响应式,文档说 FlowRouter.getParam() is reactive,因此,您可以再次摆脱所有 onCreated() 代码并使用以下代码:

    'returnLink': function (postId) {
        return '/object/' + FlowRouter.getParam('parameter') + '?firstList=' + postId;
    }
    

    试一试,告诉我进展如何。

    【讨论】:

    • 谢谢!两者结合完美!
    猜你喜欢
    • 2015-09-13
    • 2023-03-16
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多