首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >聚合物应用程序-路由子路由不会随着路由的改变而更新。

聚合物应用程序-路由子路由不会随着路由的改变而更新。
EN

Stack Overflow用户
提问于 2016-11-14 18:51:41
回答 1查看 1.4K关注 0票数 6

我的问题是链接应用程序路由。最初,我认为这个bug来自我的应用程序,但我用一个简单的例子重新创建了它。该问题源于首先访问与子路由匹配的url,然后更改路由,使其与子路由不匹配。

我不能使用聚合物cdn基标签,因为它将改变路由行为。如果您复制并粘贴运行bower init; bower install --save PolymerElements/app-route; python3 -m http.server;的代码,那么它应该运行示例代码。

问题

  1. 单击指向#/tree/maple的链接会导致routeData.collection = 'tree',subrouteData.uuid =‘枫树’。这是正确的,并按照预期的行为。
  2. 接下来,单击指向#/tree的链接将导致routeData.collection = 'tree',subrouteData.uuid = 'maple‘。注意到没有什么改变

注意,即使路径更改为#/tree ,子路由也没有更新。这是我对app-route的理解中的问题吗?

代码语言:javascript
复制
<!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>

可能的解决办法,但没有那么干净

代码语言:javascript
复制
<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>

It item会得到优先考虑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-15 00:14:30

实验表明,当路由不再匹配时,<app.route>会更改subroute,但不会清除subrouteData (可能这是该元素中的一个bug )。但是,<app-route>总是在active=true (即路由匹配)时设置data,因此在读取data之前必须检查active标志。

例如,只有当activetrue时才能显示元素(并在false时从DOM中删除它):

代码语言:javascript
复制
<template is="dom-if" if="[[subrouteActive]]" restamp>
  <my-el uuid="[[subrouteData.uuid]]"></my-el>
</template>

如果activefalse,则元素可以在内部跳过处理。

代码语言:javascript
复制
<my-el uuid="[[subrouteData.uuid]]" active="[[subrouteActive]]"></my-el>

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

或者,元素可以观察active,并在false的情况下重置

代码语言:javascript
复制
// my-el script
_onActiveChanged: function(active) {
  if (!active) {
    // reset...
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40595767

复制
相关文章

相似问题

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