我在src/cljs/project/routes.cljs根目录下的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.cljs中的::events/set-active-panel
(re-frame/reg-event-db
::set-active-panel
(fn-traced [db [_ active-panel]]
(assoc db :active-panel active-panel))),并拥有subs.cljs格式的:active-panel订阅
(re-frame/reg-sub
::active-panel
(fn [db _]
(:active-panel db)))我在我的layout.cljs中订阅:active-panel
; required [portfolio-app.subs :as subs]
(defn panel []
(let [active-panel (re-frame/subscribe [::subs/active-panel])]
[:div
"which panel? " @active-panel]))当我第一次访问页面时,@active-panel是nil。只有当我在页面中导航时,才会调度面板。我知道这在一开始是有效的。我在我的提交中看不到任何可能破坏它的东西。
如何让我的defroutes在页面加载和站点导航时触发?
发布于 2019-04-28 20:46:39
基于可用的代码,有几个猜测:
secretary/dispatch,还是仅在后续导航时调用secretary/dispatch?::events/initialize-db在正确设置:active-panel之后初始化数据库,导致您的订阅返回Are发布于 2019-04-29 01:41:57
我怀疑您已经成为重新构造"gotcha“的牺牲品,在编译过程中省略了像events这样的名称空间,因为它没有以(:require ...)形式列出。有关更多详细信息,请参阅the Gotcha documentation。
为了使(: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"))https://stackoverflow.com/questions/55887713
复制相似问题