lisp 寫前端一點也不難

github

https://github.com/HuangChen1989/cljsinone

Why

  • shadow-cljs 輕松整和了npm,安裝npm包和編譯只需幾行命令,支持live reload 和錯誤提示。
  • 使用Vue 寫前端,html 加clojurescript 很輕松。

How

  • 使用shadow-cljs 官方案例為基礎(chǔ) git clone 它
  • 可以看到這是一個npm項目,而clojurescript 依賴 Java 安裝依賴包括 nodejs Java
  • npx shadow-cljs watch app 這是啟動shadow-cljs 的命令,安裝依賴,更新包,打開nrepl, 開啟網(wǎng)頁服務(wù),一條命令完成
  • 瀏覽器打開http://localhost:8020 就能看到hello world
  • 安裝 Vue vue-router
  • 修改public/index.html
<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Browser Starter</title>
</head>

<body>
<div id="app"> 
   <router-view></router-view>
</div>

<script src="/js/main.js"></script>
<script>starter.browser.init();</script>
</body>

</html>
  • 修改src/starter/browser.cljs
(ns starter.browser
  (:require ["vue/dist/vue.js" :as Vue]
            ["vue-router/dist/vue-router.js" :as VueRouter]))

(defn ^:dev/after-load start [] 
  (.use Vue VueRouter)
  (js/console.log "start"))

(def Bar (clj->js {:template "<div>bar</div>"}))

(def routes (clj->js
               [{:path "/bar" :component Bar}]))

(def router
  (VueRouter. (clj->js {:routes routes})))

(defn app []
  (Vue.
   (clj->js
    {:el "#app"
     :router router})))

(defn ^:export init []
  (js/console.log "init")
  (start)
  (app))

(defn ^:dev/before-load stop []
  (js/console.log "stop"))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容