Based on the comments 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 "component" with ng g component {component name}
, but this didn't add anything to the build result.
I 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:
<router-outlet></router-outlet>
to the point in the shell component component where components for sub-URLs will appear (note that they are inserted into the DOM after this tag, not within it.)RouterModule.forRoot()
. This function takes an array of Route
. Each route has a path
and a component
property. path
is the url (relative to the base url) that will cause component
to be inserted into the DOM. Note that path
values should not begin with a slash.
a
tags with the routerLink
property bound to the url of your new page. Note that here, there should be a leading slash.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With