They are declared in difference places (declarations and providers). I know that service has @Injectable() to be injected to constructor of components that use that service. But why does it have to be this way? Why can't we just declare in one place? What functionalities that one can do that the other cannot?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- Service层和Dao层一定要对应吗?
- angular脚手架在ie9+下兼容问题
- k8s 访问Pod 时好时坏
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
Main Difference
"When we wanted to access the method of one component into another we have to create the object and access it. But, @Injectable tells us or methods of Service we can access just by Injecting Service in Constructor() Because of Service is Singleton I am repeating Service is Singleton. i.e. only one object of each services is available in whole application.
Example: constructor( private http: HttpClient , private toastService: ToastService)
here I'm just created variable to HttpClient type and accessing get/ post/ put methods. ToastService is my private service to access my own services.
Component
Hope you know, In AngularJS we used to write seperate script.js file for handling event, writing methods, calling api or validation and then we access that file in html like this
we uses @Component for Component. So, Component is just Like script file with added extra features. such as, We can export component and use it anywhere in application, Angular 2 Provided Object oriented features and rather than import external script, css files, they have provides support for that etc.
}
Services
We uses @Injectable for services. Services are used for common methods for some common functions across the different Component. They are simple classes with functions and members not html content. Used when - wanted to reduce duplication of code, to access or store data.
Your comment will welcome.
I'm fairly new to Angular myself but here is my understanding.
Component
From the docs:
Basically, a component is a small chunk of HTML, CSS, and Javascript that encapsulates some part of your application that you want to display.
Service
A service provides functionality that you can use across multiple parts of your application. Say you want to show specific information about a User across multiple components, but don't want to repeat the code, you would put that code into a service. You would then inject the service in your component and call the User showing code within the component, from the service.
The @Injectable() decorator is used when you want to inject other services within the service being decorated, and you do not need to include it when you are using the service in your component.
Component
It is basically a class with a decorator @Component which tells angular that the class is a component.
They are always associated with an HTML template and some CSS.
When a part of html gets repeated with a similar functionality it is always best to put it into a component. This component can be called where ever the same functionality is required.
Services
They are cenral units for some common functions across the application.
They are simple classes with functions and members.
Used when - Duplication of code exists, Access/store data.
No decorator exists for Services unlike @Component and @Directive. @Injectable is used only when one service needs to be used by a component, directive or another service.