{"flag":true,"single":true,"pageTitle":"create angular component manually and using CLI, How to add stylesheet","post":{"id":45,"user_id":"1","slug":"create-angular-component-manually-and-using-cli-how-to-add-stylesheet-m5fh","title":"create angular component manually and using CLI, How to add stylesheet","body":"<p><strong>Components <\/strong>are the building blocks that compose an application.&nbsp;<br>A component includes a<\/p>\r\n<ol>\r\n<li>TypeScript class with a @Component() decorator,<\/li>\r\n<li>An HTML template,&nbsp;<\/li>\r\n<li>styles.<\/li>\r\n<\/ol>\r\n<p>The <strong>@Component()<\/strong> decorator specifies the following Angular-specific information:<\/p>\r\n<ol>\r\n<li>A CSS selector<\/li>\r\n<li>HTML template<\/li>\r\n<li>An optional set of CSS styles that define the appearance of the template's HTML elements<\/li>\r\n<\/ol>\r\n<p>We need an html, and ts file.<\/p>\r\n<p>1. create <strong>app\/user\/user.component.html<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;h1&gt;Hello User&lt;\/h1&gt;<\/code><\/pre>\r\n<p>2. create <strong>app\/user\/user.component.ts<\/strong> , select selector and html file<\/p>\r\n<pre class=\"language-markup\"><code>import { Component } from '@angular\/core';\r\n@Component({ \/\/convert class to component\r\n  selector: 'app-user',\r\n  templateUrl: '.\/user.component.html'\r\n})\r\nexport class UserComponent {\r\n}<\/code><\/pre>\r\n<p>3. Register component in <strong>app\/app.module.ts<\/strong><\/p>\r\n<p><strong>import it&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import {UserComponent} from '.\/user\/user.component';<\/code><\/pre>\r\n<p>add in declarations<\/p>\r\n<pre class=\"language-markup\"><code>declarations: [\r\n    AppComponent, UserComponent\r\n  ],<\/code><\/pre>\r\n<p>4. Goto <strong>src\/app\/app.component.html<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;div class=\"container\"&gt;hello&lt;\/div&gt;\r\n &lt;app-user&gt;&lt;\/app-user&gt;<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>Your component will be loaded into app-user tag<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Create a component using CLI<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ng generate component Users<\/code><\/pre>\r\n<p>it will create four files , register component, in a folder Users. the .spec file is for testing purpose<\/p>\r\n<p>Just add this compoenent to your html file.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>In templateUrl we need to pass the path of html file<br><br>In template we can also path the other components<\/p>\r\n<pre class=\"language-markup\"><code>@Component({ \/\/convert class to component\r\n  selector: 'app-user',\r\n  template: '&lt;app-users&gt;&lt;\/app-users&gt;'\r\n})<\/code><\/pre>\r\n<p><strong>Stylesheet internal:<\/strong> it will applied to only current component<\/p>\r\n<pre class=\"language-markup\"><code>@Component({\r\n  selector: 'app-users',\r\n  templateUrl: '.\/users.component.html',\r\n  styles:[`\r\n    .uw{\r\n      background:blue;\r\n    }\r\n  `],\r\n})<\/code><\/pre>\r\n<p><strong>template<\/strong>: to add component&nbsp;<\/p>\r\n<p><strong>templateUrl<\/strong>: to add external component ie html<\/p>\r\n<p><strong>templateUrl<\/strong>: add css to current component<\/p>\r\n<p><strong>styleUrls<\/strong>: load external css of compoent<\/p>\r\n<p><strong>selector<\/strong>: 'app-user' , render data inside &lt;app-user&gt;&lt;\/app-user&gt; <strong>TAG<\/strong><\/p>\r\n<p><strong>selector:<\/strong>['app-user'] render data inside &lt;div app-user&gt;&lt;\/div&gt; <strong>ATTRIBUTE<\/strong><\/p>\r\n<p><strong>selector:<\/strong>'.app-user' render data inside &lt;div class=\"app-user\"&gt;&lt;\/div&gt;&nbsp; <strong>CLASS<\/strong><\/p>\r\n<p>&nbsp;<\/p>","category_id":"13","is_private":"0","created_at":"2023-03-24T05:46:09.000000Z","updated_at":"2023-03-24T06:02:06.000000Z","category":{"id":13,"user_id":"1","name":"Angular","slug":"angular-rfjp","parent_id":"12","created_at":"2023-03-24T03:25:15.000000Z","updated_at":"2023-03-24T03:25:15.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Components are the building blocks that compose an application.&nbsp;A component includes a  TypeScript class with a @Component() decorator, - create angular component manually and using CLI, How to add stylesheet (Updated: March 24, 2023) - Read more about create angular component manually and using CLI, How to add stylesheet at my programming site [SITE]","categories":[]}