我用的是秘书和试剂。这是我的密码:
(def view (atom nil))
(defn layout [view]
[:div @view])
(reagent/render-component [layout view] (.getElementById js/document "message"))
(secretary/set-config! :prefix "")
(secretary/defroute home-path "/" [query-params]
(timbre/info "Path : /, query params : " query-params)
(let [warning (:warning query-params)
success (:success query-params)
login-failed (:login_failed query-params)]
(when warning
(timbre/info "Warning found : " warning)
(reset! view [:h4 [:span.label.label-warning warning]]))
(when success
(timbre/info "Success found : " success)
(reset! view [:h4 [:span.label.label-info success]]))
(when login-failed
(timbre/info "Login failed")
(reset! view [:h4 [:span.label.label-warning "Login Failed."]]))))
(let [h (History.)]
(goog.events/listen h EventType.NAVIGATE #(secretary/dispatch! (.-token %)))
(doto h
(.setEnabled true)))忽略:前缀值(我尝试了"“、"#”,也根本没有设置:前缀),这段代码只适用于以下路由:
http://localhost:8080/login#/?success=SuccessMessage但它不适用于这样的路线:
http://localhost:8080/login?success=SuccessMessage我真正想要实现的是解析来自朋友的登录失败,如果失败,它会将我重定向到
http://localhost:8080/login?&login_failed=Y&username=someUser并向用户显示登录失败消息。我不需要为这个使用秘书,任何解析查询参数的方法对我来说都是可以的。
困难的方法是解析查询字符串,我可以使用:
(-> js/window .-location .-search)我相信有些图书馆已经做得很好了。
发布于 2015-09-15 06:21:08
我找到了。使用https://github.com/cemerick/url (为clojure和clojurescript工作),可以这样做:
(require '[cemerick.url :as url])
(:query (url/url (-> js/window .-location .-href)))发布于 2019-09-30 13:05:41
来自文档
如果URI包含查询字符串,它将自动提取到: query (用于字符串路由匹配器)和最后一个元素(用于正则表达式匹配器)。
(defroute "/users/:id" [id query-params]
(js/console.log (str "User: " id))
(js/console.log (pr-str query-params)))
(defroute #"/users/(\d+)" [id {:keys [query-params]}]
(js/console.log (str "User: " id))
(js/console.log (pr-str query-params)))
;; In both instances...
(secretary/dispatch! "/users/10?action=delete")
;; ... will log
;; User: 10
;; "{:action \"delete\"}"https://stackoverflow.com/questions/32573130
复制相似问题