ClojureScript将一个名称空间拆分为多个文件 [英] ClojureScript split one namespace into multiple files

查看:64
本文介绍了ClojureScript将一个名称空间拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了此线程,但看起来ClojureScript中没有 load load-file .是否可以将单个名称空间分隔为多个文件?

I've read this thread, but it seems like there are no load and load-file in ClojureScript. Is it possible to separate a single namespace over multiple files?

我要这样做的原因是因为我正在使用Om,并且希望将组件分离到不同的文件中.我可以使用单独的名称空间来做到这一点,但是随后我将不得不在每个文件的开头编写相同的要求,并且在主文件中调用这些组件的唯一方法是这样的:

The reason I want to do that is because I'm using Om and I want to separate components into different files. I can do it using separate namespaces, but then I will have to write the same requires in the beginning of each file and also the only way to call those components in the main file is like that:

(:require [some-project.sidebar :as sidebar])

...

(om/build sidebar/sidebar app-state)

即我必须在每个组件的名称之前指定名称空间,这看起来不太漂亮.关于如何改善它的任何想法?我是Clojure和ClojureScript的新手,所以也许我缺少明显的东西吗?

i.e. I have to specify namespace before each component's name, which doesn't look pretty. Any ideas on how to improve it? I'm new to Clojure and ClojureScript, so maybe I'm missing something obvious?

推荐答案

这里有几件事要注意

  1. 您可以在:require中使用:refer将不合格的var导入命名空间.如果有的话,这是可以的,但是如果您尝试对所有内容进行尝试,都会很快变得笨拙.
  2. Clojure应用程序通常以树状结构进行构造,其中主名称空间需要子名称空间,依此类推,因此您不必将相同的名称空间导入每个名称空间.
  3. 即使可以在多个文件之间拆分名称空间,也不是惯用的Clojure.一个文件=一个名称空间是常态.
  4. 如果需要,可以将var从一个命名空间定义为另一个命名空间,以使一个主"命名空间可以在其他命名空间中使用.
  5. 如果要最大程度地减少必须执行的导入次数,请减少名称空间,然后将其增大.

这篇关于ClojureScript将一个名称空间拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆