简体   繁体   中英

Accessing clojurescript from html

I'm using catnip/leiningen in an attempt to learn Clojure... So I have a simple website and now I'd like to add a clojure-script in my page.

So I took a simple example but now got stuck on how to access my script from my site.

My project.clj

(defproject hello-world "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [compojure "1.1.5"]
                 [hiccup "1.0.2"]]
  :plugins [[lein-ring "0.8.2"]]
  :cljsbuild {:builds
              [{:source-path "src"
                :compiler
                {:output-to "resources/public/cljs/main.js"
                 :output-dir "resources/public/cljs"
                 :optimizations :simple
                 :pretty-print true}}]}
  :ring {:handler hello-world.handler/app}
  :profiles
  {:dev {:dependencies [[ring-mock "0.1.3"]]}})

Relevant part of handler.clj

(defn header [title]
  (html 
   [:head
    [:title title]
    [:script "/cljs/play.js"]]))

If I run lein ring server there are nothing at http://localhost:8080/resources/public/cljs/main.js . How to map the requests for js so that my site can find them?

The problem is in the [:script]-tag that needed to be rewritten to either:

[:script {:src "/cljs/main.js"}]

or

(include-js "/cljs/main.js")

to generate a corrent link to the javascript-file.

Your JavaScript file should be accessible under http://localhost:8080/cljs/main.js not http://localhost:8080/resources/public/cljs/main.js . Your actual code looks to be ok otherwise.

Did you run lein cljsbuild once and did it create a JavaScript file under /resources/public/cljs/ ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM