无法匹配clojurescript-bidi中的任意路由



我在bidi中定义了路由,就像在cljs应用程序中一样:

(def routes 
["" {
"/foo" :bar
["/items" :id] :item-do}]
)
(defn- parse-url [url] (bidi/match-route routes url))
(defn- dispatch-route [matched-route]
(let [panel-name (keyword (name (:handler matched-route)))]
(dispatch [:active-panel panel-name])))
(def history (pushy/pushy dispatch-route parse-url))

当我转到路由"/foo"时,我会显示与:bar相关联的面板,但当我转到路径项目/somerandomstering时,我希望看到与:item-do相关联的板,但却看到了一个空白页,在控制台中显示:

Uncaught SyntaxError: Unexpected token '<'                                  myapp.js 1
Reference Error: myapp is not defined.                                      somerandomstring:14

我做错了什么?我该如何解决?如何正确匹配bidi中uri中的任意字符串?

--编辑--

我发现了这个:

当我匹配这个:

{
"/foo" index-handler}

它有效。

但当我匹配时

{
"/foo" [:id index-handler]
}

即使CCD_ 3输出CCD_,实际上,在浏览器中走这条路线会出现我提到的错误。

Reference Error: myapp is not defined.                                      somerandomstring:14

在控制台中,somerandomstring:14实际上是我发送回的index.html,第14行如下:

<script type="text/javascript">myapp.system.go();</script>

为什么会这样?

您的语法有点错误:

(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[bidi.bidi :as bidi] ))
(dotest
(let [routes ["" {"/foo"    :bar
"/items/" {[:id] :item-do}}]]
(spyx (bidi/match-route routes "/foo" :request-method :get))
(spyx (bidi/match-route routes "/items/abc" :request-method :get))) )

产生所需的输出:

(bidi/match-route routes "/foo" :request-method :get) 
=> {:handler :bar, 
:request-method :get}
(bidi/match-route routes "/items/abc" :request-method :get) 
=> {:route-params {:id "abc"}, 
:handler :item-do,
:request-method :get}

您可以在这个演示项目中看到更多的bidi示例。


更新#1:

你在用figwheel main吗?它比原来的figwheel要好得多。请确保使用:optimizations :none运行,因为您会得到更好的错误消息。您可能需要使用更多代码和任何新的/更改的错误消息来更新您的问题。

也可以尝试用println调试输出来分解函数调用,也许可以从最小的部分开始进行一些单元测试并向外进行。


更新#2

你的语法仍然是错误的。请注意,您需要将关键字param包装在一个向量中:

(let [foo-handler (fn [& args] "dummy-handler")
routes      ["" {"/foo/" {[:id] foo-handler}}]
result      (bidi/match-route routes "/foo/123" :request-method :get)
handler     (grab :handler result)]
(is (wild-match? {:route-params   {:id "123"}
:handler        :*
:request-method :get}
result))
(is= "dummy-handler" (handler)))

返回的值是像:foo这样的关键字还是像foo-handler这样的处理程序函数都无关紧要。result仍然具有相同的形状:

result => {:route-params {:id "123"}, 
:handler #object[tst.demo.core$fn__20970$foo_handler__21031 0x1926e42e "tst.demo.core$fn__20970$foo_handler__21031@1926e42e"], 
:request-method :get}

最新更新