Angular2模块:最佳实践(Angular2 modules: best practices)
我一直在使用一个名为
AppModule
主要模块,但我还没有将我的任何小应用程序分成几个模块。 关于Modules
的最佳实践是什么? 换句话说,何时应该重构我的代码以封装Module
某些功能?任何可能有用的例子都非常受欢迎。
干杯
I have always been working with one main module called
AppModule
but I am yet to divide any of my small apps in several modules. What are the best practises regardingModules
? In other terms, when should I refactor my code to encapsulate some functionalities in aModule
?Any examples that could help are very welcome.
Cheers
原文:https://stackoverflow.com/questions/40102835
最满意答案
首先,我同意您的评估,即请求解构通常属于路由处理程序定义。 Compojure提供Clojure解构语法的扩展这一事实似乎表明作者也有类似的感受。
您可以采取一些措施来减少解构表单中的重复。 首先,Clojure的标准地图绑定解构将有助于您发布的最后一条路线。 您可以做的第一件事是合并您从请求中提取的密钥
:params
:(POST "/" {{title :title, tags :tags, content :content, privacy :privacy} :params ...} ...)
这有点清楚,但是因为你没有重命名任何你要退出的值
:params
,你可以做得更好:(POST "/" {{:keys [title tags content privacy]} :params ...} ...)
这就是Clojure解构语法将为您提供的,但您可以利用Compojure的额外解构语法来进一步干扰它。 Compojure假设您最常感兴趣的是从请求的
:params
键获取值,因此您可以使用向量解析来将它们拉出来:(POST "/" [title tags content privacy] ...)
好多了。 由于您仍需要从请求中获取其他值,因此您可以提供
:as the-request
绑定向量末尾的请求以直接访问请求:(POST "/" [title tags content privacy :as req] ...)
但请记住,此变量也可以被解构。 考虑到所有这些,这是您上一个路线的简化版本:
(POST "/" [title tags content privacy :as {{user-email :user-email} :session}] (home-post user-email title tags content privacy))
我希望这有用! 你写的Clojure越多,你就越能找到简化代码的方法,使它更清晰,更简洁。
First off, I agree with your assessment that the request destructuring usually belongs with the route handler definitions. The very fact that Compojure provides an extension of Clojure's destructuring syntax seems to indicate that the author also felt similarly.
There are a couple of things that you can do to reduce duplication in your destructuring forms. First, Clojure's standard map binding destructuring will help with the last route you posted. The first thing that you can do is consolidate the keys that you are pulling from the request
:params
:(POST "/" {{title :title, tags :tags, content :content, privacy :privacy} :params ...} ...)
That is a little clearer, but since you are not renaming any of the values that you are pulling out of
:params
, you can do better:(POST "/" {{:keys [title tags content privacy]} :params ...} ...)
That is about as far as the Clojure destructuring syntax will get you, but you can take advantage of Compojure's additional destructuring syntax to DRY this up even more. Compojure assumes that you will most often be interested in getting values from the
:params
key of the request, so you can use vector destructuring to pull them out:(POST "/" [title tags content privacy] ...)
Much better. Since you still need to get additional values out of the request, you can supply an
:as the-request
at the end of the binding vector to access the request directly:(POST "/" [title tags content privacy :as req] ...)
Keep in mind, however, that this variable can also be destructured. With all of this in mind, here is a simplified version of your last route:
(POST "/" [title tags content privacy :as {{user-email :user-email} :session}] (home-post user-email title tags content privacy))
I hope that this is helpful! The more Clojure you write, the more ways you can find of simplifying code to make it clearer and more concise.
相关问答
更多-
Compojure解释(在一定程度上) NB。 我正在使用Compojure 0.4.1( 这里是GitHub上的0.4.1发行版)。 为什么? 在compojure/core.clj的最顶端,Compojure的目的是有用的总结: 用于生成环处理程序的简明语法。 在表面上,这就是所谓的“为什么”的问题。 要更深入一点,让我们来看一下Ring风格的应用程序的功能: 一个请求到达,并根据Ring规格转换成Clojure地图。 这个地图汇集成一个所谓的“处理函数”,这个函数可以产生一个响应(这也是一个Cloju ...
-
Compojure使用clout进行路由匹配。 这就是它允许您为每个参数指定正则表达式的方式。 以下是影响力: user=> (require '[clout.core :as clout]) user=> (require '[ring.mock.request :refer [request]]) user=> (clout/route-matches (clout/route-compile "/my-route/:mongoID.:width{\\d+}x:height{\\d+}.:extensi ...
-
好的,所以这样做的方法是使用正则表达式和上下文: (defroutes routes (context ["/:base-route" :base-route (re-pattern base-route)] [base-route] (GET "/user" [] (str "base: " base-route " user")) (GET "/settings" [] (str "base: " base-route " settings") ...
-
你是对的: (def app (wrap-defaults app-routes site-defaults)) 相当于: (def app (-> app-routes (wrap-defaults api-defaults))) 箭头称为Thread-First宏 ,允许您以线性方式编写嵌套的s表达式。 在你的第二个例子中,当my-middleware1在HTTP请求进入时调用my-middleware2是有意义的。你正在创建一个Ring Handler ,而不是直接调用中间件。 ...
-
Compojure路线问题(Compojure Routes Issues)[2021-06-27]
我想你错过了一个约束形式: (GET "/*" {params :params} (or (serve-file (params :*)) :next)) ; ^- note the binding form I think you're missing a binding form: (GET "/*" {params :params} (or (serve-file (params :*)) :next)) ; ^- note the binding form -
Compojure路线中的逗号(Commas in Compojure routes)[2023-09-09]
Compojure使用clout进行路由。 来自clout的README : Clout支持关键字和通配符。 关键字(如:title )将匹配任何字符,但以下内容: / . , ; ? / . , ; ? 。 默认情况下, clout将逗号视为路径段分隔符。 您可以通过将自定义正则表达式传递给路径来解决此问题。 以下内容:tag-names匹配除/之外的任何字符: (GET ["/tags/multiple/:tag-names" :tag-names #"[^/]+"] [tag-names] multi ... -
首先,我同意您的评估,即请求解构通常属于路由处理程序定义。 Compojure提供Clojure解构语法的扩展这一事实似乎表明作者也有类似的感受。 您可以采取一些措施来减少解构表单中的重复。 首先,Clojure的标准地图绑定解构将有助于您发布的最后一条路线。 您可以做的第一件事是合并您从请求中提取的密钥:params : (POST "/" {{title :title, tags :tags, content :content, privacy :privacy} :params ...} ...) ...
-
看待not-modified-since?的来源not-modified-since? ,我认为问题是你的请求图中没有标题,所以它会在这个expr上抛出一个NPE :( (headers "if-modified-since") 。 尝试更改您的request方法,如下所示: (defn request [resource web-app & params] (web-app {:request-method :get :headers {"content-type" "tex ...
-
您希望使用workflows/interactive-form ,其中您当前指定了workflows/interactive-login-redirect 。 后一个函数的目的是作为默认值:login-failure-handler 。 它的作用是在登录尝试失败后执行重定向; 它当然不会尝试登录用户。 您还需要从"/dashboard"路由中的friend/authenticated表单的正文中删除(resp/redirect "/signup") ,因为它无条件地将登录用户重定向到注册页面。 (重定向发生 ...
-
这对我有用: (GET "/something/:param1/:param2" [param1 param2] (str {:first param1 :second param2})) This works for me: (GET "/something/:param1/:param2" [param1 param2] (str {:first param1 :second param2}))