Skip to main content

Create a new component in angular

If you are Not aware of basis environment set up and Hello world of angular please go through below links for the context.


1 . angular-4/5/6-set-up-environment
2 . angular-5-hello-world-step-by-step


Above links help us in creating ready made application which angular CLI provide us but now we want a component of our own.


Step 1:  

Creating a Custom Component

navigate to your folder structure where you want to create your custom component
and then run below command

ng g component customHelloWorld

like this :

D:\Rakesh_kumar_Singhania\Angular5HelloWorld\hello-world-app\src\app>
ng g component customHelloWorld


This command will create your component and also it make sure to mark an entry in your app.module.ts file.

app.module.ts -- This file holds all the source required by the project which include all the imports of the components.

below is the snapshot of generated files :



Beauty of command ng g component customHelloWorld is 

This command generated 4 for the component :

1 . HTML file of your component
2. Test file for your component
3. TS file for writing your logic.
4. CSS file for styling.


Step 2:  


Open auto generated file custom-hello-world.component.ts.


@Component({
  selector: 'app-custom-hello-world',
  templateUrl: './custom-hello-world.component.html',
  styleUrls: ['./custom-hello-world.component.css']
})
export class CustomHelloWorldComponent implements OnInit {

  constructor() { }

  ngOnInit() {   
   console.log("hello");
  }
}


and and modify custom-hello-world.component.html according to your need.


<p>
  custom-hello-world works!
</p>


Below code is important in sense of mapping your HTML file and CSS file specific to this component.


@Component({
  selector: 'app-custom-hello-world',
  templateUrl: './custom-hello-world.component.html',
  styleUrls: ['./custom-hello-world.component.css']
})






So whenever you use app-custom-hello-world selector in any of your HTML it will render the
HTML part of custom-hello-world.component.html.



Lets use app-custom-hello-world  selector tag in our app.component.html.


app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
  <h1>
    Welcome to Custom component {{ title }}!
  </h1>
 <app-custom-hello-world></app-custom-hello-world>
</div>


Start the server using

 ng serve 


Hit on the url http://localhost:4200


Output :











Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...