0

我目前正在学习它reagentsecretary路线。我发现我可以用likequery-params来获取所有参数的哈希图question mark (?)?name=Daniel

(ns feampersanda.core
    (:require-macros [secretary.core :refer [defroute]])
    (:import goog.History)
    (:require
      [secretary.core :as secretary]
      [goog.events :as events]
      [goog.history.EventType :as EventType]
      [reagent.core :as r]))

;; ------------------------------
;; States
;; page --> is occupied by page state
(def app-state (r/atom {:params {}}))

;; ------------------------------
;; History

(defn hook-browser-navigation! []
  (doto (History.)
    (events/listen
     EventType/NAVIGATE
     (fn [event]
       (secretary/dispatch! (.-token event))))
    (.setEnabled true)))

;; -------------------------
;; Views


;; -------------------------
;; Parameters

(defn update-query-params! [query-params]
  (do
    (js/console.log (str query-params))
    (swap! app-state assoc-in [:params :query] query-params))
  )

;; -------------------------
;; Routing Config

(defn app-routes []
  (secretary/set-config! :prefix "#")

  (defroute "/" [query-params]
            (do
              (update-query-params! query-params)
              (swap! app-state assoc :page :home)))

  (defroute "/about/:id" [id query-params]
            (do
              (js/console.log id)
              (update-query-params! query-params)
              (swap! app-state assoc :page :about)))


  (hook-browser-navigation!))

(defmulti current-page #(@app-state :page))

(defmethod current-page :home []
  [:div [:h1 (str "Home Page")]
   [:a {:href "#/about"} "about page"]
   [:br]
   [:a {:href "#/about"} (str (:count @app-state))]
   ])

(defmethod current-page :about []
  [:div [:h1 "About Page"]
   [:a {:href "#/"} (str "home page" " --> "
                         (:yes (:query (:params @app-state)))
                         )]])

(defmethod current-page :default []
  [:div
   [:p "404"]
   ])


;; -------------------------
;; Initialize app

(defn mount-root []
  (app-routes)
  (r/render [current-page] (.getElementById js/document "app")))

(defn init! []
  (mount-root))

我不知道如何将id参数传递给 a defmethod,所以我希望它保存在一个原子中,所以我想知道如何获取包含所有命名参数的哈希映射,http://0.0.0.0:3449/#/about/black/12例如{:path "black" :id "12"}

4

1 回答 1

0

一种解决方案是使用 cemerick 的 URL 库

(require '[cemerick.url :as url])
(keys (:query (url/url (-> js/window .-location .-href))))

https://github.com/cemerick/url

于 2017-11-28T11:58:57.967 回答