zhaoda / spa

A webapp framework for routing control and view transitions
http://zhaoda.net/spa/docs/
MIT License
330 stars 92 forks source link

请问下怎么在路由后面添加参数,我在hash后面添加参数失败 #37

Closed heyxiaoling closed 8 years ago

heyxiaoling commented 8 years ago

http://localhost/php/MobileRoot/#index?name=hey&pass=123

类似这样

zhaoda commented 8 years ago

http://localhost/php/MobileRoot/#index/hey/123 http://localhost/php/MobileRoot/#index?name=hey&pass=123

On Wed, Mar 9, 2016 at 5:00 PM, heyxiaoling notifications@github.com wrote:

http://localhost/php/MobileRoot/#index?name=hey&pass=123

类似这样

— Reply to this email directly or view it on GitHub https://github.com/zhaoda/spa/issues/37.

heyxiaoling commented 8 years ago

没看懂你什么意思啊,能否讲仔细点

zhaoda commented 8 years ago

不要使用 querystring 形式的参数,而应该使用 path 形式的路由规则,在初始化视图的时候通过规则来匹配每个字段,比如你这个请求,意思是在 index 视图上传递 name 和 pass 参数,那么在创建视图的时候规则是 index/:name/:pass ,实际请求的 url 是 http://localhost/php/MobileRoot/#index/hey/123。 具体文档请参考 http://zhaoda.net/spa/docs/ 路由控制 部分

heyxiaoling commented 8 years ago

理解了,我还有个疑问,当我使用index/:num作为路由的时候,index/:1 和 index/:2会生成两个页面,而我不希望生成两个页面,只是通过num变化来做下响应

zhaoda commented 8 years ago

哦,那你就要自己来分析 url 的变化了,但是我不确定你这么做会不会触发回调函数,貌似是不会触发,因为 SPA 会认为页面路由没有发生变化,我不确定了,你最好试试。

heyxiaoling commented 8 years ago

好的,谢谢

zhaoda commented 8 years ago

不客气

On Wed, Mar 9, 2016 at 6:18 PM, heyxiaoling notifications@github.com wrote:

好的,谢谢

— Reply to this email directly or view it on GitHub https://github.com/zhaoda/spa/issues/37#issuecomment-194224420.

heyxiaoling commented 8 years ago

spa.js 1920行 ,有这么一句话,history.replaceState()会替换掉hash,这句话去掉就支持参数了,现在没发现什么问题,替换掉url有什么含义不是很理解 if(replace) { //history.replaceState(pushData, title, hash) replace默认是true

zhaoda commented 8 years ago

就是为了禁止掉在 hash 不变的情况下多次生成同一个视图,因为 SPA 认为同一个 hash 地址代表一个视图,不考虑后面 querystring,你可以修改成适合你的版本。

On Thu, Mar 10, 2016 at 4:11 PM, heyxiaoling notifications@github.com wrote:

spa.js 1920行 ,有这么一句话,history.replaceState()会替换掉hash,这句话去掉就支持参数了,现在没发现什么问题,替换掉url有什么含义不是很理解 if(replace) { //history.replaceState(pushData, title, hash) replace默认是true

— Reply to this email directly or view it on GitHub https://github.com/zhaoda/spa/issues/37#issuecomment-194724376.

heyxiaoling commented 8 years ago

恩,我这个对参数有点需求,因为刷新浏览器的的时候需要参数帮忙

zhaoda commented 8 years ago

hash 不就是用来定位的么?

On Thu, Mar 10, 2016 at 5:21 PM, heyxiaoling notifications@github.com wrote:

恩,我这个对参数有点需求,因为刷新浏览器的的时候需要参数帮忙

— Reply to this email directly or view it on GitHub https://github.com/zhaoda/spa/issues/37#issuecomment-194748600.

heyxiaoling commented 8 years ago

是可以做的,但是有参数,后台觉得更方便,所以我要改下支持参数

zhaoda commented 8 years ago

你可以给 SPA 加一个参数来控制这个事情,默认 replace 是 true,初始化 SPA的时候可以控制,然后提交 PR 给我。

On Thu, Mar 10, 2016 at 6:24 PM, heyxiaoling notifications@github.com wrote:

是可以做的,但是有参数,后台觉得更方便,所以我要改下支持参数

— Reply to this email directly or view it on GitHub https://github.com/zhaoda/spa/issues/37#issuecomment-194777942.