This is the second part in an n-part series about the JavaScript framework, Angular 6
.
In this part, we'll go over build new Angular 6 Applicatin.
This is not intended to be a complete guide, but rather an overview of the basics to get you up and running so you can get to know using Angular CLI
and how to build your Angular 6
application using it.
I assume that you have nodejs
and npm
installed. If not you have to install them first. Just Google it.
First we need to upgrade our CLI or install it for the first time. The next command will install it or upgrade your current version.
1npm i -g @angular/cli
Now We want to use CLI to create new Angular 6 Application. Change your directory where you want to create the new app, then run
1ng new angular6-series --routing --style=scss --skip-tests --service-worker
Where angular6-series
is our application's name. But we have some arguments here.
Let's Explain each argument of our command
--routing
: It builds our routes so I won't have to build them myself--style=scss
: It changes our styling extension from css
to scss
as I prefer sass.--skip-tests
: to skip creating tests files. But You may need them in this case you have to remove this argument.--service-worker
: In this article series we'll turn our application into PWA
and this argument is important for this step.But If you want to build your application normally without all these options, just run:
1ng new angular6-series
After creating our new app. Move the app directory and run the server.
1cd angular6-series 2ng serve --open
ng serve
will run the server of our app and the argument --open
will open it in the default browser.
Now you should see this in your browser
Let's create new component named home
and make it our home page with some content.
1ng g c home
This will create the component files. But you'll see no change in your app. Let's modify our src/app/app.component.html
Remove all the code and leave the last tag. It should look like
1<router-outlet></router-outlet>
Now open /src/app/app-routing.module.ts
I want to add the new component as the home page.
We need to import the HomeComponent
and add the route to routes
array.
1import { NgModule } from "@angular/core"; 2import { Routes, RouterModule } from "@angular/router"; 3import { HomeComponent } from "./home/home.component"; 4 5const routes: Routes = [ 6 { 7 path: "", 8 component: HomeComponent 9 } 10]; 11 12@NgModule({ 13 imports: [RouterModule.forRoot(routes)], 14 exports: [RouterModule] 15}) 16export class AppRoutingModule {}
Now you should see your home component content in your home page.
Let's edit our HomeComponent
open /src/app/home/home.component.html
1<h1>Welcome to our Angular 6 Application</h1> 2<p> 3 Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda repudiandae 4 repellat perferendis corrupti, harum provident ipsam illo laudantium magni 5 deleniti corporis modi error aperiam iure molestiae eligendi quo voluptatem 6 minus? 7</p>
Let's create another component named posts
1ng g c posts
First we need to add new route for our posts component
open /src/app/app-routing.module.ts
and do like we did to the home component
our file should look like
1import { NgModule } from "@angular/core"; 2import { Routes, RouterModule } from "@angular/router"; 3import { HomeComponent } from "./home/home.component"; 4import { PostsComponent } from "./posts/posts.component"; 5 6const routes: Routes = [ 7 { 8 path: "", 9 component: HomeComponent 10 }, 11 { 12 path: "posts", 13 component: PostsComponent 14 } 15]; 16 17@NgModule({ 18 imports: [RouterModule.forRoot(routes)], 19 exports: [RouterModule] 20}) 21export class AppRoutingModule {}
Now open /src/app/posts/posts.component.html
and add some html
1<div className="post"> 2 <h1>Post title here</h1> 3 <p> 4 Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia minima 5 soluta harum fugit neque accusamus non fuga blanditiis quam enim nemo, quia 6 dolores iure explicabo officia, cumque expedita quis sint. 7 </p> 8</div> 9<div className="post"> 10 <h1>Post title here</h1> 11 <p> 12 Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia minima 13 soluta harum fugit neque accusamus non fuga blanditiis quam enim nemo, quia 14 dolores iure explicabo officia, cumque expedita quis sint. 15 </p> 16</div>
You can see our new component in the browser by visiting: http://localhost:4200/posts
That's all for now.
Next: Using Angular Material with Angular 6 (soon)