What are “decorators” and how are they used?

前端 未结 5 1836
抹茶落季
抹茶落季 2020-11-28 01:07

I\'m curious what exactly decorators are in AngularJS. There isn\'t much information online for decorators save for a blurb in the AngularJS documentation and a brief (albei

5条回答
  •  萌比男神i
    2020-11-28 01:44

    Decorators allow us to separate out cross-cutting concerns and allow services to preserve the single-responsibility-principle without worrying about "infrastructure" code.

    Practical uses of decorators:

    • Caching: if we have a service which makes potentially expensive HTTP calls, we can wrap the service in a caching decorator which checks local storage before making the external call.
    • Debugging/Tracing: have a switch depending on your development/production configuration which decorates your services with debugging or tracing wrappers.
    • Throttling : wrap frequently triggered calls in a debouncing wrapper. Allows us to easily interact with rate-limited services, for example.

    In all these cases, we limit the code in the service to its main responsibility.

提交回复
热议问题