2016-12-04 45 views
0

我有這樣的my-app.html有兩個不同的網頁是主動路由問題

<app-location route="{{route}}"></app-location> 
    <app-route route="{{route}}" pattern="/:page" data="{{routeData}}" tail="{{subRoute}}"></app-route> 

這是一個相當標準的路由。

其中一個是動態加載(ALA PSK),包含my-gigs.html子頁面的:

<iron-pages id="pageSelector" role="main" selected="[[page]]" attr-for-selected="name" fallback-selection="view404"> 
     ... 
     <my-gig 
     name="gig" 
     route="{{subRoute}}" 
     </my-gig> 

     <my-city 
     name="city" 
     route="{{subRoute}}" 
     </my-city> 

my-gig.html,我希望能夠查看取決於URL特定的演出。所以,我有:

<app-route route="{{route}}" pattern="/:gigId" data="{{routeDataGig}}" active="{{activeGig}}"></app-route> 

麻煩的是,當我去到不同的網頁應用程序(比如/city/perth-au,從my-city.html顯示),路線上面活躍(!)。它將有,如gigId,perth-au(這是完全錯誤的,因爲它顯然不是一個有效的ID)。

我迷路了。每當我有路由問題之前,我會使用active標誌來檢查路由是否爲實際上處於活動狀態。在這個問題中,我遇到了問題,因爲/city/perth-au/gig/445454545都會同時滿足my-gig.html中的路由。

我在做什麼錯了......?

廣告,我通過在my-gig.html支票「解決」這個時刻:

  <iron-ajax 
      id="ajax" 
      auto 
      url="{{_makeUrl(routeDataGig.gigId)}}" 
      handle-as="json" 
      debounce-duration="300" 
      last-response="{{response}}" 
      ></iron-ajax> 

然後:

_makeUrlIfNotInfo: function(gigId) { 
    if(this.route.prefix != '/gig') return; 
    return '/stores/gigs/' + gigId; 
    } 

回答

0

我用這個問題的辦法是檢查是否頁實際上是在處理slu before之前顯示的。我利用<iron-pages>.selectedClass(其默認值爲iron-selected)並在<app-route>.data的觀察者內部檢查該類別。 <iron-pages>.selectedAttribute是一種替代方案,但需要明確的配置。

例如:

// template 
<app-route route="{{route}}" pattern="/:gigId" data="{{routeDataGig}}" active="{{activeGig}}"></app-route> 

// script 
_routeDataChanged: function(routeDataGig) { 
    const isPageSelected = this.classList.contains('iron-selected'); 
    this.gigId = isPageSelected && routeDataGig.gigId; 
} 
+0

這基本上是我在做同樣的 - 除了你正在檢查鐵選,而不是'route.prefix' ......我真的很希望我完全做的事情愚蠢,但很好,顯然我不是。 'app-route'確實看起來有根本的缺陷 – Merc