Reloading page on every click using Polymer and app-route

拜拜、爱过 提交于 2019-12-04 07:13:36

问题


I am using app-route and iron-pages with a paper-toolbar to display my views.

On one of my views, main-view, displays a randomly chosen image which changes each time the page is loaded. Every time main-view is selected from the toolbar, the page should reload so that a new image will be shown.

The problem is, if I am already at the /main-view url and I select it from the toolbar it doesn't refresh the page. Is there any way to do this?


回答1:


you should definitely add on-tap to render new images. Your image won't change, because iron-pages are observing some value (specified in selected property) so, when you click on main-view and route property already had value "main-view", observer will not trigger.

Adding on-tap on element that is handling changes will make it always trigger. Some easy example:

<iron-pages selected="{{route}}" attr-for-selected="name">
  <example-element name="main-view" on-tap="handleClick" id="main"></example-element>
  <another-element name="second-view"></another-element> 
</iron-pages>

and inside handleClick function something like:

handleClick: function() {
  this.$.main.renderImage();
}

Of course inside main-view element you can declare renderImage function which will handle rest of the logic

And don't forget to make some debounce since you don't want to propably render 20 new images in 1 second. You can use Polymer native debounce function You can read more about it here: https://www.polymer-project.org/1.0/docs/devguide/instance-methods



来源:https://stackoverflow.com/questions/42421022/reloading-page-on-every-click-using-polymer-and-app-route

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!