首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Reagent中编辑Reitit路由?

如何在Reagent中编辑Reitit路由?
EN

Stack Overflow用户
提问于 2020-07-31 23:57:04
回答 1查看 412关注 0票数 0

使用默认试剂模板创建的路线如下所示:

代码语言:javascript
复制
;; -------------------------
;; Routes

(def router
  (reitit/router
   [["/" :index]
    ["/items"
     ["" :items]
     ["/:item-id" :item]]
    ["/about" :about]]))

如果我更改了其中一个的路径(下面的“/about”改为"/test“),为什么它不再起作用?一定有别的什么东西在驱动这条路线,但我似乎想不出是什么。

代码语言:javascript
复制
;; -------------------------
;; Routes

(def router
  (reitit/router
   [["/" :index]
    ["/items"
     ["" :items]
     ["/:item-id" :item]]
    ["/test" :about]]))

这是默认的试剂模板(lein new试剂...)并且我没有在代码中更改任何其他内容。任何帮助都将不胜感激。

Edit -我在此函数的repl中查看了一些额外的细节(来自默认模板):

代码语言:javascript
复制
(defn init! []
  (clerk/initialize!)
  (accountant/configure-navigation!
   {:nav-handler
    (fn [path]
      (let [match (reitit/match-by-path router path)
            current-page (:name (:data  match))
            route-params (:path-params match)]
        (reagent/after-render clerk/after-render!)
        (session/put! :route {:current-page (page-for current-page)
                              :route-params route-params})
        (clerk/navigate-page! path)
        ))
    :path-exists?
    (fn [path]
      (boolean (reitit/match-by-path router path)))})
  (accountant/dispatch-current!)
  (mount-root))

在我看来一切都很好。实际上,在repl中执行以下步骤可以成功地将浏览器导航到正确的页面。不过,我仍然不能直接输入URL。

代码语言:javascript
复制
app:problem.core=> (require '[reitit.frontend :as reitit])
nil
app:problem.core=> (reitit/match-by-path router "/test")
{:template "/test",
 :data {:name :about},
 :result nil,
 :path-params {},
 :path "/test",
 :query-params {},
 :parameters {:path {}, :query {}}}
app:problem.core=> (def match (reitit/match-by-path router "/test"))
#'problem.core/match
app:problem.core=> (:name (:data match))
:about
app:problem.core=> (:path-params match)
{}
app:problem.core=> (def current-page (:name (:data match)))
#'problem.core/current-page
app:problem.core=> (page-for current-page)
#'problem.core/about-page
app:problem.core=> (session/put! :route {:current-page (page-for current-page) :route-params {}})
{:route {:current-page #'problem.core/about-page, :route-params {}}}
app:problem.core=>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-01 23:39:17

看起来您在src/cljs/<project_name>/core.cljs中更改了客户端的路由,但在src/clj/<project_name>/handler.clj中没有更改服务器端的路由(请查看文件底部附近的def app )。

如果您刚开始使用Clojure开发web应用程序,我建议您查看Luminus,而不是使用Reagent模板。这是一种包含更多电池的方法,有更多的文档。“使用Clojure进行Web开发”这本书的作者是同一个人(他也是Reagent的贡献者),也是推荐的读物。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63195227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档