首页 \ 问答 \ Angular2模块:最佳实践(Angular2 modules: best practices)

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 regarding Modules ? In other terms, when should I refactor my code to encapsulate some functionalities in a Module ?

Any examples that could help are very welcome.

Cheers


原文:https://stackoverflow.com/questions/40102835
更新时间:2023-06-22 18:06

最满意答案

首先,我同意您的评估,即请求解构通常属于路由处理程序定义。 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 ,而不是直接调用中间件。 ...
  • 我想你错过了一个约束形式: (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使用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}))

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)