<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Varad Shirsath</title>
    <description>The latest articles on Forem by Varad Shirsath (@vspatil87).</description>
    <link>https://forem.com/vspatil87</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3671523%2F70b75df0-e9d3-4839-870a-ea9e17455d61.png</url>
      <title>Forem: Varad Shirsath</title>
      <link>https://forem.com/vspatil87</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vspatil87"/>
    <language>en</language>
    <item>
      <title>Echarts in Angular</title>
      <dc:creator>Varad Shirsath</dc:creator>
      <pubDate>Sun, 21 Dec 2025 05:48:25 +0000</pubDate>
      <link>https://forem.com/vspatil87/echarts-in-angular-427h</link>
      <guid>https://forem.com/vspatil87/echarts-in-angular-427h</guid>
      <description>&lt;h2&gt;
  
  
  ECharts and Angular Integration Guide
&lt;/h2&gt;

&lt;p&gt;Hey👋- Hope you're having an awesome day!&lt;/p&gt;

&lt;p&gt;So, we are going to discuss how to use the echarts in angular and how to achieve the proper tree-shaking through imports. We can either directly use the echarts library or we can use the ngx-echarts wrapper. &lt;/p&gt;

&lt;h3&gt;
  
  
  When to use ECharts Directly vs. ngx-echarts Wrapper
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Direct ECharts Usage (No Wrapper)
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Full control over initialization and lifecycle&lt;/li&gt;
&lt;li&gt;No abstraction layer overhead&lt;/li&gt;
&lt;li&gt;Direct access to all ECharts APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual resize handling required&lt;/li&gt;
&lt;li&gt;More boilerplate code&lt;/li&gt;
&lt;li&gt;You handle memory cleanup directly&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ngx-echarts Wrapper
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handles Angular lifecycle automatically&lt;/li&gt;
&lt;li&gt;Built-in resize observer&lt;/li&gt;
&lt;li&gt;Less boilerplate&lt;/li&gt;
&lt;li&gt;Better integration with Angular patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Additional abstraction layer&lt;/li&gt;
&lt;li&gt;Less control over specific scenarios&lt;/li&gt;
&lt;li&gt;Dependencies on wrapper updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recommendation:&lt;/strong&gt; Use direct ECharts for custom dashboards requiring fine-grained control; use ngx-echarts for standard applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Proper Imports and Tree-Shaking
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ INCORRECT (Includes Everything)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as echarts from 'echarts';  // Bundle: ~700-900 KB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This imports the entire ECharts library regardless of what you use, resulting in massive bundle sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ CORRECT (Tree-Shakable)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as echarts from 'echarts/core';
import { BarChart, LineChart } from 'echarts/charts';
import { 
  GridComponent, 
  TooltipComponent, 
  LegendComponent, 
  TitleComponent 
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

// Register only what you use
echarts.use([
  BarChart, 
  LineChart, 
  GridComponent, 
  TooltipComponent, 
  LegendComponent, 
  TitleComponent,
  CanvasRenderer
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bundle Size Comparison:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before (incorrect): ~900 KB&lt;/li&gt;
&lt;li&gt;After (correct): ~80-100 KB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Savings: 91% reduction&lt;/strong&gt; 🚀&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why echarts.use() is Critical
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;echarts.use()&lt;/code&gt; function registers only the components you need. Without it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Larger bundles:&lt;/strong&gt; Everything gets included&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slower load times:&lt;/strong&gt; Users download unnecessary code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poor optimization:&lt;/strong&gt; Tree-shaking cannot work effectively&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of it like ordering a whole library when you only need one book.&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript Type Support
&lt;/h3&gt;

&lt;p&gt;To get IDE autocomplete for chart options while maintaining tree-shaking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type { EChartsOption } from 'echarts/types';

const option: EChartsOption = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [120, 200, 150],
      type: 'line'
    }
  ]
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Use &lt;code&gt;import type&lt;/code&gt; (not just &lt;code&gt;import&lt;/code&gt;) to ensure the type is stripped during compilation and doesn't end up in your bundle.&lt;/p&gt;

&lt;p&gt;Happy coding! 🎉&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Service Levels in Angular</title>
      <dc:creator>Varad Shirsath</dc:creator>
      <pubDate>Sat, 20 Dec 2025 07:05:12 +0000</pubDate>
      <link>https://forem.com/vspatil87/service-levels-in-angular-5f1g</link>
      <guid>https://forem.com/vspatil87/service-levels-in-angular-5f1g</guid>
      <description>&lt;h2&gt;
  
  
  Understanding Service Levels in Angular! 🚀
&lt;/h2&gt;

&lt;p&gt;Hey👋- Hope you're having an awesome day! &lt;/p&gt;

&lt;p&gt;So, we're diving into something that always confuses people when they're starting with Angular - Services. I know, I know... services can feel like this mysterious box where you throw code and hope it works the way you expect. But trust me, once you understand the different levels where services live, everything starts to click.&lt;/p&gt;

&lt;p&gt;So here's what I want to talk about: What are the different levels where we can use services in Angular? And more importantly, why does it even matter?&lt;/p&gt;

&lt;p&gt;Let's break it down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Root-Level Services: The Global MVP 🌍
&lt;/h3&gt;

&lt;p&gt;Alright, so when you use the Angular CLI to generate a service - you know, that ng generate service command - Angular gives you this by default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable({
  providedIn: 'root'
})
export class UserService { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here? Angular is saying: "Hey, I'm going to create ONE single instance of this service and share it across your entire application."&lt;/p&gt;

&lt;p&gt;Think about it like a shared resource. Imagine your app is a hotel, and this service is like the front desk. No matter which guest (component) walks up, they're talking to the same front desk person. There's only one person, and everyone gets the same information from them.&lt;/p&gt;

&lt;p&gt;Why would you use this?&lt;/p&gt;

&lt;p&gt;Perfect for things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authentication Service - You want to share login state across all components&lt;/li&gt;
&lt;li&gt;Configuration Service - Global app settings that don't change&lt;/li&gt;
&lt;li&gt;HTTP/API Service - A single point for all backend communication&lt;/li&gt;
&lt;li&gt;State Management - Shared data that multiple components need&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is your go-to for most services. It's simple, efficient, and prevents you from creating unnecessary instances.&lt;/p&gt;

&lt;h3&gt;
  
  
  Component-Level Services: Keep It Local 🏠
&lt;/h3&gt;

&lt;p&gt;Now, what if you want a service that's only used by one component? Or what if each component should have its own separate copy of the service?&lt;br&gt;
That's where component-level services come in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-shopping-cart',
  templateUrl: './shopping-cart.component.html',
  providers: [CartService]
})
export class ShoppingCartComponent { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the thing: Every time this component is created, a brand new instance of CartService is created just for that component.&lt;/p&gt;

&lt;p&gt;Back to our hotel analogy - this is like having a private caretaker for each guest room. Room 101 has their own caretaker, Room 102 has a different one. They don't share information.&lt;/p&gt;

&lt;p&gt;When would you actually need this?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Form State - Each instance of a form component keeps its own data&lt;/li&gt;
&lt;li&gt;Component Specific Logic - Data that should never leak to other components&lt;/li&gt;
&lt;li&gt;Multiple Instances - When the same component appears multiple times on a page, each needs independent behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine you have a ProductFilterComponent that appears on multiple pages. Each one needs to remember its own filters independently. Component-level services are perfect for this!&lt;/p&gt;

&lt;h3&gt;
  
  
  Module-Level Services: The Middle Ground ⚖️
&lt;/h3&gt;

&lt;p&gt;Okay, so you're probably working with standalone components in modern Angular, but if you're using modules, there's also a module-level option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@NgModule({
  declarations: [...],
  imports: [...],
  providers: [AuthGuardService]
})
export class AdminModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's the deal here?&lt;br&gt;
It depends on whether the module is eagerly loaded or lazy-loaded:&lt;/p&gt;

&lt;p&gt;Eagerly Loaded Module:&lt;br&gt;
If your module loads when the app starts, the service behaves like a singleton across the whole app. It's pretty much the same as &lt;code&gt;providedIn: 'root'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lazy-Loaded Module:&lt;br&gt;
Here's where it gets interesting! When Angular lazily loads a module (like a route that only loads when you visit it), it creates a completely separate injector. This means the service gets a new instance just for that module.&lt;/p&gt;

&lt;p&gt;So if your app has an "Admin Dashboard" that only loads when an admin logs in, you could have an AdminService that only exists in that context. It's clean, efficient, and keeps unrelated code separate.&lt;/p&gt;

&lt;p&gt;// Routes configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  { path: 'admin', loadChildren: () =&amp;gt; import('./admin/admin.module').then(m =&amp;gt; m.AdminModule) }
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, the AdminService lives only within the lazy-loaded admin module. Once the module is unloaded, the service instance is gone too.&lt;/p&gt;

&lt;p&gt;So... Which One Should You Use? 🤔&lt;br&gt;
Real talk? It comes down to your app's architecture and how you want data to flow.&lt;/p&gt;

&lt;p&gt;Ask yourself these questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is this service needed everywhere? → Use Root-Level ✅&lt;/li&gt;
&lt;li&gt;Is this service only used by one component? → Use Component-Level ✅&lt;/li&gt;
&lt;li&gt;Is this service scoped to a specific feature module that lazy-loads? → Use Module-Level ✅&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The secret sauce is understanding how Angular's &lt;strong&gt;Dependency Injection&lt;/strong&gt; system works under the hood. When you know how the injector resolves dependencies - how it searches up the component tree, how it handles hierarchies - that's when you'll know exactly where to put your services.&lt;/p&gt;

&lt;p&gt;The Bottom Line 💡&lt;br&gt;
Services in Angular are just well-organized pieces of code that handle specific jobs. The level at which you provide them determines who gets access and how many copies exist.&lt;br&gt;
Pick the right level, and your app stays maintainable and performant. Pick the wrong level, and... well, let's just say debugging gets fun. 😅&lt;/p&gt;

&lt;p&gt;Key takeaways to remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root-level = One instance, shared everywhere&lt;/li&gt;
&lt;li&gt;Component-level = New instance per component&lt;/li&gt;
&lt;li&gt;Module-level = Depends on eager vs. lazy loading&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding! 🎉&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
