【问题标题】:Get all of named routes获取所有命名路由
【发布时间】:2017-11-28 22:50:30
【问题描述】:

我目前正在学习reagent,以secretary 作为其路线。我发现我可以使用query-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 参数传递给defmethod,所以我希望它保存在一个原子中,所以我想知道如何获取包含所有命名参数的哈希映射喜欢http://0.0.0.0:3449/#/about/black/12{:path "black" :id "12"}

【问题讨论】:

    标签: clojurescript reagent secretary


    【解决方案1】:

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

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

    https://github.com/cemerick/url

    【讨论】:

    • 感谢您的回复。这是关于解析参数的新知识,但您在上面使用的代码正在获取 :query 哈希映射,我只能在 secretary 中使用 query-params 参数来完成此操作。我在我的问题中添加了一些词,即我想将named parametershttp://localhost:3449/about/:id/:path 变成{:id "blah" :path "blah"}。对不起,如果我的话不好解释
    • 您能否在您的 URL 示例中包含您希望找到“blah”的位置。如果没有主题标签,则将在您的后端处理程序中查找斜线(例如 compojure)。我不知道处理 URL 中的键值对比使用查询参数更好的方法。
    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 2016-07-07
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多