1

我的routes.cljs文件中定义了应用程序路由,位于src/cljs/project/routes.cljs.

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

  (defroute
    "/" []
    (re-frame/dispatch [::events/set-active-panel :welcome-panel])))
  ; shortened for brevity

它被初始化为core.cljs

; required [portfolio-app.events :as events]
(defn ^:export init []
  (routes/app-routes)
  (re-frame/dispatch-sync [::events/initialize-db])
  (dev-setup)
  (mount-root))

它被发送::events/set-active-panelevents.cljs

(re-frame/reg-event-db
 ::set-active-panel
 (fn-traced [db [_ active-panel]]
   (assoc db :active-panel active-panel)))

并且:active-panel订阅了subs.cljs

(re-frame/reg-sub
 ::active-panel
 (fn [db _]
   (:active-panel db)))

我订阅:active-panel了我的layout.cljs

; required [portfolio-app.subs :as subs]
(defn panel []
  (let [active-panel (re-frame/subscribe [::subs/active-panel])]
    [:div
     "which panel? " @active-panel]))

@active-panelnil我第一次访问页面的时候。仅当我浏览页面时才调度面板。我知道这最初是有效的。我在我的提交中看不到任何可能破坏它的东西。

如何让我defroutes在页面加载以及通过网站导航时触发?

4

2 回答 2

0

基于可用代码的几个猜测:

  • 您是secretary/dispatch在页面加载还是在后续导航中调用?
  • 您是否在正确设置::events/initialize-db后初始化数据库:active-panel,导致您的订阅返回nil
于 2019-04-28T12:46:39.323 回答
0

我怀疑您已经成为重新框架“陷阱”的受害者,events在编译过程中省略了类似名称空间,因为它没有在(:require ...)表单中列出。有关更多详细信息,请参阅Gotcha 文档

为了(:require ...)更明确和更难忘记,我总是将所有(reg-event-* ...)调用包装在一个从主程序初始化的更大函数中:

(ns demo.core  
  (:require 
     [demo.events :as events]
     [demo.subs :as subs]
     ...))

(defn app-start
  "Initiates the cljs application"
  []
  (events/define-all-events!)
  (subs/initialize-all)
  (configure-browser-routing!)
  ...)

接着:

(ns demo.events ...)
(defn define-all-events! []
  (reg-event-db ...)
  (reg-event-fx ...)
  ...)

我为秘书/会计路由以及定义订阅(即reg-sub)使用了类似的“将其包装在一个函数中”技术。例如:

(defn configure-browser-routing! []
  (println "configure-browser-routing - enter")

  (secretary/defroute "/all" []
    (println :secretary-route-all)
    (browser-nav-handler :all))
  (secretary/defroute "/active" []
    (println :secretary-route-active)
    (browser-nav-handler :active))
  (secretary/defroute "/completed" []
    (println :secretary-route-completed)
    (browser-nav-handler :completed))
  (secretary/defroute "/*" []
    (println ":secretary-route-all  *** default ***")
    (browser-nav-handler :all))

  (accountant/configure-navigation!
    {:nav-handler  (fn [path]
                     (t/spy :accountant--nav-handler--path path)
                     (secretary/dispatch! path))
     :path-exists? (fn [path]
                     (t/spy :accountant--path-exists?--path path)
                     (t/spy :accountant--path-exists?--result
                       (secretary/locate-route path)))})
  (println "configure-browser-routing - leave"))
于 2019-04-28T17:41:57.100 回答