4

如何从 Reitit 后端 Clojure 路由中获取路径参数?我正在尝试通过:id以下方式获取与关联的 val,但在 REPL 中不断收到 404 file not found 错误。

["/foo/:id" {:get 
  (fn [{:keys [path-params ]}]
   (some-ns/some-fn (:id path-params)))}]

我尝试使用https://luminusweb.com/docs/routes.htmlhttps://github.com/metosin/reitit/blob/master/README.md上的文档。

4

1 回答 1

4

从请求中解构路径参数,然后提取 id。

(defn project-routes
  "SPA routing"
  []
  ["" {:middleware [middleware/wrap-base
                    middleware/wrap-formats]}

   ["/foo"
    ["/bar/:id"
     {:get (fn [{:keys [path-params] :as _req}]
             (http-response/ok 
                   (core/READ some-table 
                      (Integer/valueOf (:id path-params)))))}]]])

https://github.com/luminus-framework/luminus-docs/blob/master/resources/md/sessions_cookies.md获得的文档和示例。如https://clojureverse.org/t/how-do-you-accomplish-spa-re-frame-teardown/5516/6所述,使用了控制器。

于 2020-03-24T22:36:48.920 回答