我在一个具有以下结构的项目上使用来自https://github.com/PolymerElements/app-route的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>
...因此,访问/路径将显示一个欢迎页面,并访问/loja/categoria,my-app将调用store-main-page,后者将调用category-page。
我的问题是,我想在显示store-main-page之前对用户进行身份验证,如果用户没有经过身份验证,就将他重定向到/上的欢迎页面。有没有一种方法可以使用app-route实现这一点?
我发现使用app-route更改页面的唯一方法是调用
this.set('route.path', 'path');但这在链式路由上不起作用,在我的情况下,这将导致重定向到/loja/路径,这不是我想要的。
如何更改父元素上的路由?这有可能吗?
发布于 2016-07-25 11:55:35
我刚刚意识到app-location只是iron-location和app-route之间的一个接口。
因此,我解决了向子元素添加iron-location并更改其path属性的问题。
确切的代码是:
<iron-location id="ironLocation"></iron-location>
...
</template>
<script>
...
attached: function() {
if (!userAuth()) {
this.$.ironLocation.set('path', '/');
} else {
...
}
}
...我不知道这是否是理想的解决方案,但它是有效的。
https://stackoverflow.com/questions/38556552
复制相似问题