【问题标题】:How to use Specter in ClojureScript?如何在 ClojureScript 中使用 Spectre?
【发布时间】:2016-08-24 12:33:24
【问题描述】:

我正在努力在 cljs 中使用幽灵 (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!)]}})

【问题讨论】:

  • 您的project.clj 文件长什么样?

标签: clojure namespaces clojurescript specter


【解决方案1】:

FWIW 让 Spectre 在 clojurescript 中运行时遇到了一些困难,但现在它似乎运行良好。困难在于必须引入两个命名空间并确定 Spectre 的哪一部分位于哪个命名空间中。 Nathan Marz 说他想unify them eventually,但在那之前我的命名空间看起来像这样:

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

所以,和你的没什么不同!但至少看起来你可以排除你的命名空间defn。顺便说一句,这对我来说也适用于无花果。我通过lein new devcards test 使用Devcards 作为起点,然后从那里使用project.clj

【讨论】:

    【解决方案2】:

    现在 Spectre 可以在 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)
    

    【讨论】:

    • 我建议不要将幽灵别名为s,因为这封信是commonly used 作为clojure.spec.alpha 的别名。
    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多