{"id":435,"date":"2018-03-11T21:19:08","date_gmt":"2018-03-11T15:49:08","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=435"},"modified":"2018-03-12T10:54:31","modified_gmt":"2018-03-12T05:24:31","slug":"angular-5-app-setup-routing","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/","title":{"rendered":"Angular 5 app setup and Routing"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In this blog I am going to show you how to setup a Angular 5 app and how to create routing with its components. As you all know Angular is the powerful framework of Javascript, and it is basically a front end development computer language for SPA&#8217;s (Single Page Application).<\/p>\n<p>Before I start about this setup process, assuming that you all have installed <a href=\"https:\/\/nodejs.org\/en\/download\/\"><em><strong>Node JS<\/strong><\/em><\/a> and NPM(Node Package Manager) in your machine as NPM will be our key feature to install Angular CLI(Command Line Interface). What is CLI? If you don&#8217;t know about CLI you can go through my previous blog where I have explained about CLI by clicking <em><strong><a href=\"http:\/\/blog.openwebsolutions.in\/cli-command-line-interface-used-latest-js-frameworks\/\">this link<\/a>.<\/strong><\/em><\/p>\n<p>If you haven&#8217;t installed <a href=\"https:\/\/nodejs.org\/en\/download\/\"><em><strong>Node JS<\/strong><\/em><\/a> yet, install it first before install Angular app.<\/p>\n<p>&nbsp;<\/p>\n<h2>Install Angular<\/h2>\n<p>&nbsp;<\/p>\n<p>Open command prompt (cmd) for Windows, Terminal for Linux \/ MacOS. As I\u00a0am using\u00a0a windows machine,\u00a0will use command prompt here.<\/p>\n<p><strong>Step 1<\/strong><\/p>\n<p>Open cmd, and type<strong> <em>node -v<\/em><\/strong> to\u00a0view the version of installed node and type <em><strong>npm -v<\/strong>\u00a0<\/em>to view the version of npm. If everything is ok. then install Angular CLI globally by typing\u00a0<em><strong><span class=\"pln\">npm install <\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">g <\/span><span class=\"lit\">@angular<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">cli<\/span><\/strong><span class=\"pln\">\u00a0<\/span><\/em>(-g for globally installation)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong><\/p>\n<p>Go to the project directory where you want to store your project, (<em>eg. cd var\/www\/html\/my-angular-project)\u00a0<\/em>and create a new project by typing the command <em><strong>ng new my-first-angular-app,\u00a0<\/strong><\/em>which will take some times to create the project and the skeleton of application (it will install all its dependencies)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong><\/p>\n<p>Your installation is completed. Now go to the project directory by typing <em><strong>cd my-first-angular-app\u00a0<\/strong><\/em>and serve the project on your browser by typing the command\u00a0<em><strong><span class=\"pln\">ng serve <\/span><span class=\"pun\">&#8212;<\/span><span class=\"pln\">open .<\/span><\/strong><span class=\"pln\">\u00a0<\/span><\/em>If the app doesn&#8217;t run automatically to the browser, you can directly go to the browser and type\u00a0<em><strong>http:\/\/localhost:4200\/\u00a0<\/strong><\/em>(4200 is the port on which the app is running). Yay! now you can see your app is running like the following<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-442\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2018\/03\/Untitled.png\" alt=\"angular app running\" width=\"325\" height=\"378\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2018\/03\/Untitled.png 325w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2018\/03\/Untitled-258x300.png 258w\" sizes=\"(max-width: 325px) 100vw, 325px\" \/><\/p>\n<p>You can edit your app by changing the files <em><strong>app.component.ts<\/strong><\/em>, <em><strong>app.component.html<\/strong><\/em> or <em><strong>app.component.css\u00a0<\/strong><\/em>(Please note: If you edit any file and hit save CTRL+S, it will automatically compile and reload\/refresh the app).<\/p>\n<p>&nbsp;<\/p>\n<h2>Routing between Components<\/h2>\n<p>&nbsp;<\/p>\n<p><strong>Step 1<\/strong><\/p>\n<p>Create a file\u00a0<em><strong>app.routes.ts\u00a0<\/strong><\/em>within <em><strong>src\/app<\/strong><\/em> folder. Open the file in any code\/text editor and edit the file. Type the following codes in app.routes.ts file. We are assuming that all the components (.ts files) are within the same folder (src\/app)<\/p>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><\/code><\/pre>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> NgModule <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'@angular\/core';\r\n<\/span><\/code>import <span class=\"token punctuation\">{<\/span> RouterModule, Routes <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'@angular\/router'<\/span><span class=\"token punctuation\">;<\/span><\/pre>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> HomeComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/home.component'<\/span><span class=\"token punctuation\">;<\/span> \r\n<span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> AboutComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/about.component'<\/span><span class=\"token punctuation\">;\r\n<\/span><\/code>import <span class=\"token punctuation\">{<\/span> ProductComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/product.component'<\/span><span class=\"token punctuation\">; \r\n<\/span>import <span class=\"token punctuation\">{<\/span> ContactComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/contact.component'<\/span><span class=\"token punctuation\">;<\/span> \r\nimport <span class=\"token punctuation\">{<\/span> ErrorComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/error.component'<\/span><span class=\"token punctuation\">; <\/span><\/pre>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><span class=\"token keyword\">export<\/span> <span class=\"token keyword\">const<\/span> routes<span class=\"token punctuation\">:<\/span> Routes <span class=\"token operator\">=<\/span> <span class=\"token punctuation\">[<\/span>\r\n<span class=\"token punctuation\">{<\/span> path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">''<\/span><span class=\"token punctuation\">, redirectTo: 'home',<\/span> pathMatch<span class=\"token punctuation\">:<\/span> 'full' <span class=\"token punctuation\">},<\/span>\r\n<span class=\"token punctuation\">{<\/span> path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">'home'<\/span><span class=\"token punctuation\">,<\/span> component<span class=\"token punctuation\">:<\/span> HomeComponent <span class=\"token punctuation\">},\r\n<\/span><\/code>{ path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">'about'<\/span><span class=\"token punctuation\">,<\/span> component<span class=\"token punctuation\">:<\/span> AboutComponent <span class=\"token punctuation\">}, <\/span> \r\n{ path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">'product\/:id'<\/span><span class=\"token punctuation\">,<\/span> component<span class=\"token punctuation\">:<\/span> ProductComponent <span class=\"token punctuation\">}, <\/span> \r\n{ path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">'contact'<\/span><span class=\"token punctuation\">,<\/span> component<span class=\"token punctuation\">:<\/span> ContactComponent <span class=\"token punctuation\">}, <\/span> \r\n{ path<span class=\"token punctuation\">:<\/span> <span class=\"token string\">'**'<\/span><span class=\"token punctuation\">,<\/span> component<span class=\"token punctuation\">:<\/span> ErrorComponent <span class=\"token punctuation\">}<\/span><\/pre>\n<pre class=\" language-javascript\">]<span class=\"token punctuation\">;\r\n\r\n<\/span><\/pre>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"><span class=\"token keyword\">@NgModule({<\/span>\r\n<span class=\"token punctuation\">imports<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">[  RouterModule.forRoot(routes)  ],<\/span>\r\nexports: [  RouterModule  ]<\/code><\/pre>\n<pre class=\" language-javascript\">})\r\n\r\nexport class AppRoutingModule {\r\n\r\n}<\/pre>\n<pre class=\" language-javascript\"><span class=\"token punctuation\">\u00a0<\/span><\/pre>\n<p class=\" language-javascript\"><span class=\"token punctuation\">Now open <em><strong>app.module.ts<\/strong><\/em> file, and add the routing component to the app.module.ts by the following method.<\/span><\/p>\n<pre class=\" language-javascript\"><span class=\"token punctuation\">..\r\n..\r\nimport { AppRoutingModule } from '.\/app.routes';\r\n..\r\n..\r\n\r\n@NgModule({\r\n..\r\n..\r\nimports: [\r\n ..,\r\n AppRoutingModule,\u00a0\r\n ..\r\n],\r\n})\r\n<\/span><\/pre>\n<pre class=\" language-javascript\"><code class=\" language-javascript\"> \r\n<\/code><\/pre>\n<p><strong>Step 2<\/strong><\/p>\n<p>Now open <em><strong>app.component.html\u00a0<\/strong><\/em>to edit for app router outlet (The dynamic section where all the routes will load dynamically). Edit the file like the following<\/p>\n<pre>&lt;header&gt;&lt;\/header&gt;\r\n&lt;!-- <em>all routing components will be visible at the below section dynamically<\/em> --&gt;\r\n&lt;router-outlet&gt;&lt;\/router-outlet&gt;\r\n&lt;footer&gt;&lt;\/footer&gt;\r\n\r\n<\/pre>\n<p>Now you have an Angular project with its routes. You can view the files by providing the full <em><strong>url<\/strong><\/em> with the routes names as suffix. For example<\/p>\n<pre>http:<a href=\"http:\/\/localhost:4200\/\">\/<\/a>\/localhost:4200\/home\r\nhttp:\/\/localhost:4200\/about\r\nhttp:\/\/localhost:4200\/product\/55\r\nhttp:\/\/localhost:4200\/contact\r\nhttp:\/\/localhost:4200\/abcd \/*This will load the ErrorComponent as there is no \"abcd\" route present*\/<\/pre>\n<p>Or, you can view the routes by linking them with &lt;a&gt; anchor element tag such as following example<\/p>\n<pre><span class=\"tag\">&lt;<\/span><span class=\"tag\">a<\/span> <span class=\"atn\">routerLink<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"\/home\"<\/span><span class=\"tag\">&gt;<\/span><span class=\"pln\">Home<\/span><span class=\"tag\">&lt;\/<\/span><span class=\"tag\">a<\/span><span class=\"tag\">&gt;<\/span>\r\n<span class=\"tag\">&lt;<\/span><span class=\"tag\">a<\/span>\u00a0<span class=\"atn\">routerLink<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"\/about\"<\/span><span class=\"tag\">&gt;<\/span><span class=\"pln\">About us<\/span><span class=\"tag\">&lt;\/<\/span><span class=\"tag\">a<\/span><span class=\"tag\">&gt;<\/span>\r\n<span class=\"tag\">&lt;<\/span><span class=\"tag\">a<\/span>\u00a0<span class=\"atn\">routerLink<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"\/product\/55\"<\/span><span class=\"tag\">&gt;Products<\/span><span class=\"tag\">&lt;\/<\/span><span class=\"tag\">a<\/span><span class=\"tag\">&gt;<\/span>\r\n<span class=\"tag\">&lt;<\/span><span class=\"tag\">a<\/span>\u00a0<span class=\"atn\">routerLink<\/span><span class=\"pun\">=<\/span><span class=\"atv\">\"\/contact\"<\/span><span class=\"tag\">&gt;<\/span><span class=\"pln\">Contact us<\/span><span class=\"tag\">&lt;\/<\/span><span class=\"tag\">a<\/span><span class=\"tag\">&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>That&#8217;s all<\/p>\n<p><strong><em>Please share if you like it!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In this blog I am going to show you how to setup a Angular 5 app and how to create routing with its components. As you all know Angular is the powerful framework of Javascript, and it is basically a front end development computer language for SPA&#8217;s (Single Page Application). Before I start about [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":436,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,3,5],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Angular 5 app setup and Routing - Openweb Solutions Blog<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular 5 app setup and Routing - Openweb Solutions Blog\" \/>\n<meta property=\"og:description\" content=\"&nbsp; In this blog I am going to show you how to setup a Angular 5 app and how to create routing with its components. As you all know Angular is the powerful framework of Javascript, and it is basically a front end development computer language for SPA&#8217;s (Single Page Application). Before I start about [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-11T15:49:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-03-12T05:24:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2018\/03\/banner.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"497\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2018\/03\/banner.jpg\",\"width\":1024,\"height\":497,\"caption\":\"angular 5 app setup and routing\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/\",\"name\":\"Angular 5 app setup and Routing - Openweb Solutions Blog\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/#primaryimage\"},\"datePublished\":\"2018-03-11T15:49:08+00:00\",\"dateModified\":\"2018-03-12T05:24:31+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/615090f954d8460cf6186cf920b47398\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/angular-5-app-setup-routing\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/615090f954d8460cf6186cf920b47398\",\"name\":\"Preetam Mallick\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ed836329485e177373e4ff5323b6670a?s=96&r=g\",\"caption\":\"Preetam Mallick\"},\"description\":\"Frontend developer at Openweb Solutions\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/435"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":14,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/435\/revisions\/451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/436"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}