簡體   English   中英

如何在ClojureScript中使用Spectre?

[英]How to use Specter in ClojureScript?

我正在努力在cljs中使用Spectre(0.12.0)。 我可能沒有正確設置ns。

運行一個簡單的示例,我得到以下錯誤消息:

=> (select [:a :b] {:a {:b 42}})
#object[TypeError TypeError: Cannot read property 'specter' of undefined]

我的ns看起來像這樣:

(ns dummy.core
  (:require [com.rpl.specter :as s])
  (:require-macros [com.rpl.specter.macros :refer [select]]))

編輯

它實際上可以在瀏覽器中工作。

但是它沒有出現在Figwheel的REPL中,我很自然地首先嘗試了它。

我的project.clj (為簡便起見,略有縮短)

(defproject dummy "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.8.0" :scope "provided"]
                 [org.clojure/clojurescript "1.7.228" :scope "provided"]
                 [reagent "0.5.1"]
                 [com.rpl/specter "0.12.0"]]

  :plugins [[lein-cljsbuild "1.1.1"]]
  :min-lein-version "2.5.0"
  :clean-targets ^{:protect false}
    [:target-path
      [:cljsbuild :builds :app :compiler :output-dir]
      [:cljsbuild :builds :app :compiler :output-to]]

  :resource-paths ["public"]

  :cljsbuild {:builds {:app {:source-paths ["src"]
                             :figwheel {:on-jsload dummy.core/fig-reload}
                             :compiler {:main dummy.core
                                        :output-to "public/js/app.js"
                                        :output-dir "public/js/out"
                                        :source-map-timestamp true
                                        :asset-path   "/js/out"
                                        :optimizations :none
                                        :pretty-print  true}}

                       :min {:source-paths ["src"]
                             :compiler {:main dummy.core
                                        :output-to "public/js/app.js"
                                        :optimizations :whitespace}}}}

  :figwheel { :http-server-root "public"
              :server-port 3449
              :nrepl-port 7002
              :nrepl-middleware ["cemerick.piggieback/wrap-cljs-repl"]
              :css-dirs ["public/css"]}

  :profiles { :dev { :dependencies [[prone "1.0.2"]
                                    [lein-doo "0.1.6"]
                                    [pjstadig/humane-test-output "0.7.1"]
                                    [lein-figwheel "0.5.0-6"]
                                    [org.clojure/tools.nrepl "0.2.12"]
                                    [com.cemerick/piggieback "0.2.1"]]

                     :plugins [[lein-figwheel "0.5.0-6"]
                               [lein-doo "0.1.6"]]

                     :doo {:build "test"}

                     :injections [(require 'pjstadig.humane-test-output)
                                  (pjstadig.humane-test-output/activate!)]}})

FWIW我遇到了一些顛簸,使Spectre陷入了clojurescript中,但現在看來工作正常。 艱難的道路圍繞着必須引入兩個名稱空間並弄清楚Spectre的哪個位在哪個名稱空間中。 內森·馬茲(Nathan Marz)說過,他希望最終將它們統一起來 ,但是直到那時,我的命名空間如下所示:

(ns test.core
  (:require [com.rpl.specter :as sp])
  (:require-macros [com.rpl.specter.macros :refer [select setval] :as spm]))

因此,與您的沒什么不同! 但是至少看起來您可以排除名稱空間defn。 順便說一句,這對我來說在figwheel也很好。 我通過lein new devcards testDevcards用作起點,並從那里開始使用project.clj

現在幽靈可以在Clojurescript中使用如下

(ns test.core
  [com.rpl.specter :as s])

(def breakpoints ["320px" "600px" "1280px"])

(def styles {:padding-top ["20px" "30px" "40px" "50px"]
             :margin ["60px"]})


(s/transform [s/MAP-VALS] #(%1 0) styles)

(s/setval [s/MAP-VALS s/FIRST] s/NONE styles)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM