2016-07-24 59 views
2

我使用從https://github.com/PolymerElements/app-route該應用路由元件上具有以下結構的一個項目:聚合物,鏈式應用路由元件上改變路線

包含以下代碼甲my-app元件:

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

<iron-pages selected="[[page]]" attr-for-selected="name"> 
    <store-main-page 
    name="loja" 
    route="{{subroute}}" 
    categories="[[categories]]"> 
    </store-main-page> 
    ... 
</iron-pages> 
... 

一個store-main-page元素有:

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

<iron-pages selected="[[page]]" attr-for-selected="name"> 
    <category-page 
    name="categoria" 
    route="{{subroute}}" 
    selected-category="{{selectedCategory}}" 
    cart="{{cart}}"> 
    </category-page> 
    ... 
</iron-pages> 
... 

因此,訪問/路徑的歡迎頁面將顯示一個nd訪問/loja/categoriamy-app將調用store-main-page,這將調用category-page

我的問題是,我想在顯示store-main-page之前對用戶進行身份驗證,如果用戶未通過身份驗證,請將其重定向到/上的歡迎頁面。有沒有辦法使用app-route來做到這一點?

我發現用app-route是調用

this.set('route.path', 'path'); 

改變頁面但這並不上鍊路線的工作,在我的情況的唯一方法,這將導致對重定向到/loja/路徑,這是不是有什麼我想要。

如何更改父元素上的路線?那可能嗎?

回答

3

我剛纔發現app-location只是iron-locationapp-route之間的接口。

所以我解決了這個問題,添加一個iron-location到我的子元素,並更改其path屬性。

確切的代碼是:

<iron-location id="ironLocation"></iron-location> 
... 
</template> 

<script> 
... 
    attached: function() {      
    if (!userAuth()) {    
     this.$.ironLocation.set('path', '/');    
    } else { 
     ... 
    }          
    } 
... 

我不知道這是否是理想的解決方案,但它的工作原理。

相關問題