如何从子元素更改应用程序路由位置

How to change app route location from child element

我创建了基于应用程序路由的项目。某些事件我需要将路由更改为不同的根目录。

index.html

<my-app></my-app>

我的-app.html

<!-- this app-route manages the top-level routes -->
<app-route
    route="{{route}}"
    pattern="/:view"
    data="{{routeData}}"
    tail="{{subroute}}"></app-route>

<!-- iron-pages selects the view based on the active route -->
<iron-pages selected="[[routeData.view]]" attr-for-selected="name">
  <landing-app name="home" route="{{subroute}}"></landing-app>
  <dashboard-app name="dashboard" route="{{subroute}}"></dashboard-app>
</iron-pages>

登陆-app.html

当调用处理程序时,我需要更改到仪表板的路由。怎么做?

<dom-module id="landing-app">
  <template>
    <button on-click="_handlerCall">Change to Dashboard</button>
  </template>
  <script>
    class LandingApp extends Polymer.Element {

      static get is() {return 'landing-app'}

      _handlerCall() {
        this.set('route.path', '/dashboard') // but no luck :(
      }
    }
    customElements.define(LandingApp.is, LandingApp);
  </script>
</dom-module>

添加: <app-location route="{{route}}"></app-location><template> 登陆之后-app.html

<dom-module id="landing-app">
  <template>
    <app-location route="{{route}}"></app-location>
    <button on-click="_handlerCall"> Change to Dashboard</button>
  </template>
  <script>
    class LandingApp extends Polymer.Element {

      static get is() {return 'landing-app'}

      _handlerCall() {
        this.set('route.path', '/dashboard') // :)
      }
    }
    customElements.define(LandingApp.is, LandingApp);
  </script>
</dom-module>

app-location 的文档: https://www.webcomponents.org/element/PolymerElements/app-route/elements/app-location