我已经在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关联的面板,但是当我转到路由items/somerandomstring时,我希望看到与: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]
}尽管(bidi/match-route routes "/foo/abc")输出{:handler index-handler, :route-params {:id "abc"}},但实际上在浏览器中转到此路由会产生我提到的错误。
Reference Error: myapp is not defined. somerandomstring:14在控制台中,somerandomstring:14实际上是我要发回的index.html,第14行如下:
<script type="text/javascript">myapp.system.go();</script>为什么会这样呢?
发布于 2020-03-28 07:23:42
您的语法有一点错误:
(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}您可以在this demo project中看到更多bidi示例。
更新#1:
你在使用figwheel-main吗?它比原来的图形轮要好得多。确保使用: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}https://stackoverflow.com/questions/60895127
复制相似问题