【问题标题】:Meteor JS Router.go redirect to the same pageMeteor JS Router.go 重定向到同一页面
【发布时间】:2015-07-13 13:22:30
【问题描述】:

我有一个问题,在我的流星应用程序中,当我在一个页面上说“测试/两个”时,我有一个名为“全部”的按钮,现在当我点击这个按钮时,我希望我的流星应用程序重新加载整个“ test/two”页面,其中包含来自服务器的初始数据(有一些过滤器和您可以在“test/two”上更改的内容)。

问题是,无论我使用锚点还是点击事件来重定向 Router.go 流星都会识别出我已经在这个页面上并且什么都不做。

我可以强制流星应用程序从服务器重新加载我已经打开的同一页面吗?

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    看起来并不容易。
    问题是,如果您完全刷新页面,您将获得包含初始数据的页面,但您会丢失会话。
    示例:
    .js

    if (Meteor.isClient) {
        Session.setDefault('something', "other");
        console.log(Session.get('something'));
        Template.reload.events({
            'click #reload': function() {
                document.location.reload(true);
            },
            'click #change': function() {
                Session.set('something', 'else');
                console.log(Session.get('something'));
            }
        });
    }
    
    Router.route('/', function() {
        this.layout('layout');
        this.render('reload');
    });
    

    .html

    <head>
        <title>Reload</title>
    </head>
    <body>
        <h1>Reload test!</h1>
    </body>
    
    <template name="layout">
        {{> yield}}
    </template>
    
    <template name="reload">
        <button id ="change" type="button">Change Session</button>
        <br/><br/>
        <input type="text">
        <input type="checkbox">
        <input type="checkbox" checked>
        <button id="reload" type="button">Reload</button>
    </template>
    

    我用 Iron:router 做了一些实验,但没有得到任何结果。
    一个蛮力的想法可以将初始数据保存在对象中,如果用户按下“全部”按钮,您将使用侦听器恢复原始数据。

    另外我发现了这个,How do I reload the current iron:router route?,看看,也许有用。

    【讨论】:

      【解决方案2】:

      如果你只想重新加载整个页面,Iron:Router 不是必需的,你可以使用纯 JavaScript:

      document.location.reload(true);
      

      【讨论】:

        猜你喜欢
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 2015-10-22
        • 2020-06-27
        相关资源
        最近更新 更多