Based on the ments on another of my questions (gradle how to add files javascript fies to a directory in the war file) I'm trying to use angular-cli to help build and manage an angular project. However, I cannot seem to find any documentation on how to create a second webpage in the project, which to me seems like a very basic task. I tried creating a "ponent" with ng g ponent {ponent name}
, but this didn't add anything to the build result.
Based on the ments on another of my questions (gradle how to add files javascript fies to a directory in the war file) I'm trying to use angular-cli to help build and manage an angular project. However, I cannot seem to find any documentation on how to create a second webpage in the project, which to me seems like a very basic task. I tried creating a "ponent" with ng g ponent {ponent name}
, but this didn't add anything to the build result.
- 3 You should go though tutorial. Routing (navigating between pages) is covered in detail here: angular.io/docs/ts/latest/tutorial/toh-pt5.html – Sasxa Commented Mar 13, 2017 at 5:11
1 Answer
Reset to default 13I had missed the section of the angular docs on routing since I did not make the connection between the word "routing" and what I wanted to do. Routing as described here works perfectly when using Node as your server. However, other web servers such as Tomcat (which I am using for this project) will not since ng build
only generates an index.html file. Node knows that it should re-route URLs under the angular base to that file, but Tomcat doesn't. A proxy server such as apache needs to be placed in front of the Tomcat server to redirect the urls to the base url for the application.
With that out of the way, here is the basics of routing:
- create a ponent for each "page" (the ponent does not need to be responsible for the whole page displayed. see 2)
- create a "shell" ponent that contains features that will be on all pages e.g. toolbar, side navigation.
- add
<router-outlet></router-outlet>
to the point in the shell ponent ponent where ponents for sub-URLs will appear (note that they are inserted into the DOM after this tag, not within it.) - in the imports for your module, add
RouterModule.forRoot()
. This function takes an array ofRoute
. Each route has apath
and aponent
property.path
is the url (relative to the base url) that will causeponent
to be inserted into the DOM. Note thatpath
values should not begin with a slash. - add
a
tags with therouterLink
property bound to the url of your new page. Note that here, there should be a leading slash.