[关闭]
@Wahson 2016-12-18T18:03:26.000000Z 字数 5544 阅读 997

Route

前端技术



1、URL结构(链接

  1. 在浏览器中输入url,在控制台可以获取location对象的值:
  2. window.location =
  3. {
  4. "hash":"#traffic-conditions",
  5. "search":"?type=daily",
  6. "pathname":"/weather/90014",
  7. "port":"",
  8. "hostname":"www.example.com",
  9. "host":"www.example.com",
  10. "protocol":"http:",
  11. "origin":"http://www.example.com",
  12. "href":"http://www.example.com/weather/90014?type=daily#traffic-conditions",
  13. "ancestorOrigins":{}
  14. }

查看Location API

2、Polymer 自带路由组件的原理

polymer主要通过以下组件实现路由:<app-location> <app-route> <iron-location> <iron-query-params>

  1. <!- Polymer提供的地址解析控件 ->
  2. <app-location route="{{route}}" use-hash-as-path></app-location>
  3. <!- app-location内置了iron-location,iron-location主要是负责解析url ->
  4. <iron-location
  5. path="{{__path}}"
  6. query="{{__query}}"
  7. hash="{{__hash}}"
  8. url-space-regex={{urlSpaceRegex}}>
  9. </iron-location>
  10. <!-
  11. path = window.location.pathname
  12. query = window.location.search.slice(1)
  13. hash = window.location.hash.slice(1)
  14. ->
  15. <iron-query-params
  16. params-string="{{__query}}"
  17. params-object="{{queryParams}}">
  18. </iron-query-params>
  19. <!-
  20. iron-query-params负责把 "param1=abc&param2=def"
  21. 解析成对象 queryParams = { param1: "abc", param2: "def"}
  22. ->

原理很简单,iron-location中监听了window.location的变化,然后把URL解析成path、query、hash

  1. //上例中,path、query、hash的值如下
  2. path="/weather/90014"
  3. query="type=daily"
  4. hash="traffic-conditions"

接着< app-location>组装route对象,并向上notify。

  1. route =
  2. {
  3. "prefix":"",
  4. "path":"traffic-conditions",
  5. "__queryParams":{
  6. "type":"daily"
  7. },
  8. }
  9. 注意这里path的值是hash的值,因为在app-location中,当使用use-hash-as-path时,path使用hash的值。

然后根据route对象的值,和pattern匹配出要跳转的页面。

  1. <app-route route="{{route}}" pattern="/:page" data="{{data}}" tail="{{tail}}"></app-route>

但是单纯使用polymer的路由方案并不能完全满足现时的需求。

3、< ks-route> 需求

  • 路由控件使用要方便
  • 通过给定uri可以跳转到任意页面,并且uri中可以传递参数
  • 页面间跳转,能够很方便传递参数
  • 能够区分页面是第一次进入还是返回
  • 需要有权限控制,如未登录时,通过uri访问订单页面需要重定向到登录页面

4、< ks-route> 方案

1、采取所有页面都是一级页面的方案,不管多少级的页面,直接通过页面名称完成跳转。i.e.
AppRoute.show('center-index',param);
AppRoute.show('order-payment',param);

2、使用Polymer的路由组件<app-route><app-location><iron-pages>,在这三个组件的基础上进行封装实现<ks-route><ks-location>

3、浏览器直接定位资源方式:页面后直接带上参数
http://127.0.0.1:8088/main.html#/test-order-page?orderNo=orderNo12

查看代码

路由数据流动

1、from 一个页面(page1) to 另一个页面(page2)

page1ks_routeapp_routeiron_pagesks_location调用: show(page2)更新app_route绑定的rtdata更新显示的页面进行页面权限检查,然后调用页面定义的display方法更新location绑定,更新 urlpage1ks_routeapp_routeiron_pagesks_location

注意:app-route和ks-location的数据绑定和更新都在show()方法里触发

2、页面返回

page2ks_route调用back()call:history.back()page2ks_route

调用history.back()后,url会发生改变:

ks_locationks_routeiron_pagesurl changedfire route-change eventfire query-param-change eventalt[ 页面改变 ][ 页面参数改变 ]更新显示的页面进行页面权限检查,然后调用页面定义的display方法ks_locationks_routeiron_pages

3、url 直接进入页面
与调用history.back()后的过程相同。

使用方法

  1. //页面声明
  2. <ks-route id="appRoute" is-root>
  3. <section id="main-page">
  4. </ks-route>
  5. <script>
  6. const AppRoute = document.querySelector("#appRoute");
  7. </script>
  1. //页面跳转
  2. AppRoute.show(`${pageName}`,param);
  3. //页面返回
  4. AppRoute.back();
  1. //main-page.html
  2. Polymer({
  3. is:'main-page',
  4. //check authority before display
  5. preCheck(resolve, reject) {
  6. //do something here
  7. //if preCheck passed, then return resolve()
  8. //else return reject()
  9. //you can also pass a closure as an argument to reject method
  10. //i.e. reject(() => console.log("preCheck not passed");)
  11. return resolve();
  12. },
  13. //this method will be called with an argument({param:{isReturn: false, yourParamKey: yourParamValue}}) when this page is displayed
  14. display({param:{isReturn, orderNo}}={}){
  15. if(isReturn) console.log("page return occured");
  16. if (!!orderNo) this.set("orderNo", orderNo);
  17. },
  18. back() {
  19. return AppRoute.back();
  20. },
  21. //display order-page
  22. showOrderPage(){
  23. AppRoute.show("order-page",{orderNo:123});
  24. }
  25. });

5、不足

  • 不容易定制页面的切换效果
  • ...

6、其他

  • 页面html和js中混杂了路由跳转的东西,需要处理,工作量不少
  • ...
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注