首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免在重定向后重新加载页面

避免在重定向后重新加载页面
EN

Stack Overflow用户
提问于 2015-01-20 15:51:28
回答 1查看 729关注 0票数 0

使用聚合物0.4.2和app-router

这是我的路由表

代码语言:javascript
复制
 <app-router>

    <app-route path="/" import="landing/landing-page.html"></app-route>

    <app-route path="/balance" import="balance/balance-view.html"></app-route>
    <app-route path="/balance/:year/:month" import="balance/balance-view.html"></app-route>
    <app-route path="/balance/:year/:month/:day" import="balance/day-view/day-view.html"></app-route>

    <app-route path="*" import="landing/landing-page.html"></app-route>

</app-router>

当调用/balance时,我需要确定当前的年份和月份,并重定向到/balance/year/month。我已经尝试过router.go()window.location来做这个重定向。

如果我这样做,balance-view组件将第二次包含在第一个实例之下,导致布局中断。我目前看到的解决这个问题的唯一方法是window.location.reload()。然而,这会在应用程序流中造成严重的延迟。

问题出在哪里&如何解决呢?

EN

回答 1

Stack Overflow用户

发布于 2015-01-20 23:46:00

经过一番努力,我让它正常工作了:

相同的app-router配置:

代码语言:javascript
复制
 <app-router>

  <app-route path="/" import="landing/landing-page.html"></app-route>

  <app-route path="/balance" import="balance/balance-view.html"></app-route>
  <app-route path="/balance/:year/:month" import="balance/balance-view.html"></app-route>
  <app-route path="/balance/:year/:month/:day" import="balance/day-view/day-view.html"></app-route>

  <app-route path="*" import="landing/landing-page.html"></app-route>

</app-router>

然后,在balance/balance-view.html中:

代码语言:javascript
复制
<polymer-element name="balance-view" attributes="year month">
  <template>
    <h1>Balance view</h1>
    <h2>Year: {{year}}</h2>
    <h2>Month: {{month}}</h2>
  </template>
  <script>   
    Polymer("balance-view", {
      domReady: function() {
        console.log(this.pathArg1);
        if (this.year === undefined) {
          var d = new Date();
          var month = new Array();
          month[0] = "January"; month[1] = "February"; month[2] = "March"; month[3] = "April";
          month[4] = "May"; month[5] = "June"; month[6] = "July"; month[7] = "August";
          month[8] = "September"; month[9] = "October"; month[10] = "November"; month[11] = "December";
          var currentYear =  d.getFullYear();
          var currentMonth = month[d.getMonth()];
          document.querySelector('app-router').go('/balance/'+currentYear+'/'+currentMonth);
        }
      }
    });
  </script>
</polymer-element>

domReady函数中,如果我检测到没有传递任何year,我会计算当前年份和月份,然后执行以下操作:

代码语言:javascript
复制
document.querySelector('app-router').go('/balance/'+currentYear+'/'+currentMonth);

你可以看到它在this Plunker中运行,代码是here

希望它能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28040130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档