聚合物app-route子路由不会随路由更改而更新

cos*_*ouc 6 javascript polymer polymer-1.0

我的问题涉及链接app-route.最初我认为这个bug来自我的应用程序,但我用一个简单的例子重新创建它.问题来自于首先访问与子路由匹配的URL,然后更改路由以使其与子路由不匹配.

我不能使用Polymer cdn基本标记,因为它会改变路由的行为.如果你复制并粘贴代码运行bower init; bower install --save PolymerElements/app-route; python3 -m http.server;它应该运行示例代码.

问题

  1. 单击链接以#/tree/maple导致routeData.collection ='tree',subrouteData.uuid ='maple'.这是正确的,并且表现如预期
  2. 接下来单击链接#/tree导致routeData.collection ='tree',subrouteData.uuid ='maple'.注意没有变化

请注意,即使更改为子路径的路径#/tree 未更新.这是我理解的问题app-route吗?

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">

  <link rel="import" href="./bower_components/app-route/app-route.html">
  <link rel="import" href="./bower_components/app-route/app-location.html">
  <link rel="import" href="./bower_components/polymer/polymer.html">
</head>

<body>
  <x-example></x-example>
</body>

</html>

<dom-module id="x-example">
  <template>
    <style>
    </style>
    <app-location route="{{route}}" use-hash-as-path></app-location>
    <app-route route="{{route}}" pattern="/:collection" data="{{routeData}}" tail="{{subroute}}"></app-route>
    <app-route route="{{subroute}}" pattern="/:uuid" data="{{subrouteData}}"></app-route>

    <h1>Path</h1> 
    <p>route: [[routeData.collection]]</p>
    <p>subroute: [[subrouteData.uuid]]</p>

    Visit: [In Order]
    <a href="#/tree/maple">[2] Collection [TREE] UUID [MAPLE]</a> ->
    <a href="#/tree">[1] Collection [TREE]</a> 
  </template>

  <script>
    Polymer({
      is: "x-example",
    });
  </script>
</dom-module>
Run Code Online (Sandbox Code Playgroud)

可能的解决方案但不干净

<app-route route="{{route}}" pattern="/:collection" data="{{listData}}" active="{{listActive}}"></app-route>
<app-route route="{{route}}" pattern="/:collection/:uuid" data="{{itemData}}" active="{{itemActive}}"></app-route>
Run Code Online (Sandbox Code Playgroud)

它item会得到偏好.

ton*_*y19 5

实验表明,当路由不再匹配时,<app.route>更改subroute但不清楚subrouteData(可能这是该元素中的错误).但是,<app-route>总是设置data何时active=true(即路线匹配),因此您必须active在阅读前检查标志data.

例如,你可以仅显示一个元素active是true(从DOM时将其删除false):

<template is="dom-if" if="[[subrouteActive]]" restamp>
  <my-el uuid="[[subrouteData.uuid]]"></my-el>
</template>
Run Code Online (Sandbox Code Playgroud)

或者元素可以在内部跳过处理,如果active是false:

<my-el uuid="[[subrouteData.uuid]]" active="[[subrouteActive]]"></my-el>

// my-el script
_processUuid: function() {
  if (!this.active) return;
  // do something with this.uuid...
}
Run Code Online (Sandbox Code Playgroud)

或者元素可以在以下情况下观察active和重置false:

// my-el script
_onActiveChanged: function(active) {
  if (!active) {
    // reset...
  }
}
Run Code Online (Sandbox Code Playgroud)