DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest JavaScript Topics

article thumbnail
Defining Implementations of Services: Dependency Injection in Angular
I was working on a practical project using Angular 8, where I was creating a service and thinking more about how to implement SOLID principles. I decided to have an interface to define my service and a class to implement the logic. Everything was okay with that design, but a question came to my mind, "How can I implement dependency injection with my Angular component?" In this example, I am going to use an abstract class, Let's create an abstract class, named IGreetingsService. This will be used as an Interface with a greeting method. TypeScript x 1 export abstract class IGreetingsService { 2 constructor() { } 3 abstract greeting(): String; 4 } Now, let's create a class, GreetingsServiceImpl. This class implements the IGreetingsService abstract class. TypeScript xxxxxxxxxx 1 1 export class GreetingsServiceImpl implements IGreetingsService { 2 constructor() { } 3 greeting(): String{ 4 return "Pruebaa"; 5 }; 6 } At this point, we have a regular class to use as a service, but we need to add the annotation @Injectable. We have to put it at IGreetingsService. TypeScript x 10 1 @Injectable({ 2 providedIn: 'root', 3 useClass: GreetingsServiceImpl, 4 }) 5 export abstract class IGreetingsService { 6 7 constructor() { } 8 9 abstract greeting(): String; 10 } The magic is at the property useClass. This class is used by the Angular framework to know what concrete class has to use for dependency injection when you are using theIGreetingsService abstract class. So we are ready to use our service into an angular component. TypeScript xxxxxxxxxx 1 12 1 @Component({ 2 selector: 'my-app', 3 templateUrl: './app.component.html', 4 styleUrls: [ './app.component.css' ] 5 }) 6 export class AppComponent { 7 name: String; 8 constructor(private service: IGreetingsService ) { 9 this.name = this.service.greeting(); 10 } 11 12 } At the moment, to create an instance of the AppComponent, Angular automatically creates an instance of GreetingsServiceImpl , instead of IGreetingsService. (Remember IGreetingsService is an abstract class). This approach can help you in several situations; for example, think about a service using REST services to get some data, but now there is a new requirement, and you need to get the same data from session storage or local storage. You can create a new class implementing your abstract class and update the property useClass to refer to your new class. Another situation would be if you needed a different implementation on production environments and testing environments. You can find the complete source code here: https://stackblitz.com/edit/angular8-injectiondependency-interface. A final note, you can define a provider in the component definition annotation to inject a specific implementation of your service. TypeScript x 1 @Component({ 2 selector: 'my-app', 3 templateUrl: './app.component.html', 4 styleUrls: [ './app.component.css' ], 5 providers :[{ provide: IGreetingsService, useClass: GreetingsServiceImpl }] 6 }) Further Reading Angular: Everything You Need to Know [Tutorials]. About Dependency Injection.
March 9, 2020
by Javier Santos
· 9,421 Views · 4 Likes
article thumbnail
Integration Dotenv With NestJS and Type ORM
In this article, we discuss how to integrate dotenv with NestJs and TypeORM to get you up and going with your Node application configuration.
March 5, 2020
by Jason Rees
· 24,732 Views · 5 Likes
article thumbnail
10 Famous Apps Using React.js
See ten famous applications that are using React.js nowadays, including Facebook, WhatsApp, and more.
March 4, 2020
by Manish Patel
· 22,324 Views · 4 Likes
article thumbnail
Observability on Heroku: How to Monitor Apps on Managed Infrastructure
Although the services Heroku offers are fully managed services, there are a number of ways to monitor your infrastructure natively or with add-ons.
March 3, 2020
by Michael Bogan DZone Core CORE
· 8,330 Views · 3 Likes
article thumbnail
For Loops in JavaScript: Native, forEach, and For-of
In this article, we cover how to loop over array in JavaScript with a traditional, native loop, ES6's forEach method, and a for-of loop.
March 2, 2020
by Peter Connelly
· 12,160 Views · 5 Likes
article thumbnail
Geospatial Data Analysis in Angular
This article will explain the method to create an interactive geospatial data visualization with Angular 8, MapboxGL and DeckGL.
March 2, 2020
by Imaginea Technologies
· 8,501 Views · 3 Likes
article thumbnail
Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
This article demonstrates how you can select an image using the Angular 8 UI, send it to the Spring backend, and retrieve it later from MySQL.
February 26, 2020
by Rida Shaikh
· 128,752 Views · 6 Likes
article thumbnail
Understanding Microservices Choreography Using RabbitMQ and Node.js
We take a brief look at the concept of microservices choreography and how this would work in an app using RabbitMQ and Node.js.
Updated February 20, 2020
by Danesh Hussain Zaki
· 20,929 Views · 3 Likes
article thumbnail
Data Binding and Server-Side Rendering in Angular and React
In this article, we discuss how data binding and server-side rendering differs in both Angular and React.
February 19, 2020
by Hiren Dhaduk
· 9,943 Views · 6 Likes
article thumbnail
Angular Image Gallery With Bootstrap
Create a quick Flickr image gallery from scratch with images of varying dimensions using Angular and Bootstrap.
February 14, 2020
by Jyoti Barik
· 31,671 Views · 2 Likes
article thumbnail
How WebDriverIO Uses Selenium Locators in a Unique Way
Learn how WebDriverIO is transforming the way of Selenium locator strategy and how easy to remember and write it.
February 13, 2020
by Ramit Dhamija
· 12,843 Views · 1 Like
article thumbnail
13 of the Best React JavaScript Frameworks
The following are 13 of the best React JavaScript frameworks; all are open source. React Native is used for building native applications.
February 12, 2020
by Amit Dua
· 15,297 Views · 11 Likes
article thumbnail
Extending Vue.js Components
Using HTML, and a little bit of Pug, this web dev expert shows us how to extend the components of our Vue.js applications.
Updated February 10, 2020
by Anthony Gore
· 37,662 Views · 5 Likes
article thumbnail
Drop jQuery From Your Bootstrap Project (and Replace it With Vue.js!)
Tired of the baggage that comes along with using jQuery in your development projects, such as increased size and trick configurations? Use Vue.js instead.
Updated February 10, 2020
by Anthony Gore
· 22,467 Views · 6 Likes
article thumbnail
7 Ways to Define a Component Template in Vue.js
Having a hard time deciding on which method to use to define your component template? Read on for some great tips from a Vue.js expert.
Updated February 10, 2020
by Anthony Gore
· 31,170 Views · 3 Likes
article thumbnail
Don't Forget Browser Button UX In Your Vue.js App
A Vue.js whiz explains how to create different kinds of browser navigation using this powerful JavaScript framework. Read on for more!
Updated February 10, 2020
by Anthony Gore
· 10,891 Views · 4 Likes
article thumbnail
Pre-Render a Vue.js App (With Node Or Laravel)
In this article, we'll explore how pre-rendering works with Vue.js and look at two examples; one with a Node.js project, one with a Laravel project.
Updated February 10, 2020
by Anthony Gore
· 23,673 Views · 4 Likes
article thumbnail
4 Ways To Boost Your Vue.js App With Webpack
If you're looking to give your Vue.js-based web application a little bit of an enhancement, read on to see how Webpack can help with that.
Updated February 10, 2020
by Anthony Gore
· 18,934 Views · 3 Likes
article thumbnail
3 Code Splitting Patterns For Vue.js and Webpack
In this article, a DZone MVB and Vue.js expert shows three methods you can use for architecting an app for code splitting using Vue and Webpack.
Updated February 10, 2020
by Anthony Gore
· 14,036 Views · 3 Likes
article thumbnail
Build A Collapsible Tree Menu With Vue.js Recursive Components
Ever been reading a comment section of a site and wonder how they make that collapsible, indented style view? Read on to find out!
Updated February 10, 2020
by Anthony Gore
· 21,033 Views · 8 Likes
  • Previous
  • ...
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×