【问题标题】:Unable to match arbitrary routes in clojurescript bidi无法匹配 clojurescript bidi 中的任意路由
【发布时间】:2020-07-08 17:32:36
【问题描述】:

我已经在 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 相关联的面板,但是当我转到路线项目/somerandomstring 时,我希望看到与:item-do 相关联的面板,但是看到一个空白页,并在控制台中显示:

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

我做错了什么,我该如何解决?如何正确匹配双向 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>

这是为什么?

【问题讨论】:

    标签: routing clojurescript bidi


    【解决方案1】:

    你的语法有点错误:

    (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 中查看更多比迪烟示例。


    更新 #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}
    

    【讨论】:

    • 我想我找到了问题的核心,但我还不知道如何解决它。检查我的编辑。我也改为:优化:无,但这并没有改变错误消息
    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2020-04-14
    • 1970-01-01
    • 2017-03-07
    • 2017-01-21
    • 2020-09-20
    相关资源
    最近更新 更多