Tuesday, 5 May 2020

Build Your Own Blog - Create Your First Component and Component Driven Design

<< Your First Angular Project                                  Table of Contents                            Create the BlogPost Component >>

In this post we are going to build our first component using Angular. If you haven't already, please read the previous post to set up the prerequisites required.

(If you're new here, I recommend visiting the Table of Contents)

I will show you how to use the Angular CLI (ng command) to add a new component, download and apply a font and apply simple styling to our component and home page.

We will call our header component 'blogheader'. For now, it will contain only the title of the page "kite .net", more will be added later.



When choosing colours for your web page, it is advisable to pick colours form the same scheme. The reason is that we want the colours to compliment each other so that they appear aesthetically pleasing to our user. Read more about this here: colour schemes

Component Driven Design

The idea is that we arrange our website into components. Think of a component as a group of html, css and javascript designed for a specific purpose. In our case, we have Header, Post and Archive components.

These components will be modular that are re-usable and can be initialised multiple times on a website. This will reduce code duplication and complexity. Everything we need for a component is contained only within that component alone.

A component consists of the following parts:
  • Selector - The name of the element to instantiate the component)
  • Template (Html) - The layout of a component. i.e. How the data will be displayed.
  • Style (CSS/SASS) - The styling required for this component
  • TypeScript - The logic behind the initialisation of the component. i.e. Fetching the data required for the template.
Click here to read more about: Component Driven Development

If you're familiar with the Model-View-Controller (MVC) design pattern, it is very similar.

BlogHeader Component

1. Add a new component by running ng generate component blogheader


You will now see the following added to the project.



2. Download this font (zip) from Google Fonts: https://fonts.google.com/specimen/Allerta+Stencil



3. Create a new folder assets > fonts

4. Unzip the font to the fonts folder:

 

3. Open app > styles.sass and add the following:



This will allow us to refer to our font in our css as 'stencil'

4. Open blogheader.component.sass and add the following:









This will define our header class using our 'stencil' font plus other font attributes.

5. Open blogheader.component.html and add the following:




Applying the header class will apply the font, size and colour.


6. Open app.component.sass and add the following class:







The container class will be used for creating a central page layout across all components.

10. Finally, run your application locally using ng serve and see the results.


Well done! We've created our first component using Angular!

<< Your First Angular Project                                  Table of Contents                            Create the BlogPost Component >>

No comments:

Post a Comment