
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.4 memoryHistory
2.4.1 创建memoryHistory
可通过调用createMemoryHistory创建memoryHistory:

对于内存路由,其运行环境通常不在浏览器内,一般作为测试使用或如React Native原生环境。在创建memoryHistory时,除了history配置,如keyLength、getUserConfirmation,还可传入initialEntries、initialIndex,其声明如下:

注意,basename在memoryHistory中不被支持。
initialEntries类似于Browser Router或Hash Router的历史栈,它确定了初始化的栈内容。由于是内存路由,这个历史栈仅能由history库进行记录。initialIndex表示初始的栈指针位置。它们默认的值如下:

initialEntries的默认值为拥有初始入口“/”的一个栈记录,initialIndex的默认值为0。
对于memoryHistory,其除了通用的history的属性,还多出index、entries和canGo属性。

entries为历史栈数组,比起browserHistory与hashHistory,memoryHistory能获取所有的历史记录,如上一个导航地址、第一个导航地址等。index为当前历史栈指针的指针位置,需要获取当前的地址,可从history.entries[history.index]中获取,其也等价于history.location。history.length即等价于entries.length。对于memoryHistory,其canGo属性用来判断跳转位置n是否可以跳转。
