I am going through the tutorials for Angular 2 and have been able to get a simple application up and running. Now, I am moving on to the routing and navigation part found here https://angular.io/docs/ts/latest/guide/router.html, but something isn't working for me.
I went to the live example in Plunker and downloaded the code from there. I then setup a solution in Visual Studio 2015 and plugged in all of the code from the Plunker download. The application works perfectly except for one thing, the navigation doesn't seem to work as the documentation would seem to indicate.
I start debugging the application in Visual Studio using IIS Express and Chrome browser. The main page loads correctly and I can click on the links for Crisis Center and Heroes. When I click the links, the component views correctly load and everything looks perfect.
Now, if I try to navigate by simply typing in the URL, the component views don't load and all I have is a blank page.
For example, I start debugging the application in Visual Studio and the Chrome Browser opens with the URL http://localhost:56074/. The main page is loaded correctly with the "Component Router" header and the two links for "Crisis Center" and "Heroes". Now, if I simply go to the address bar and add "/crisis-center" to the end of the URL so it looks like http://localhost:56074/crisis-center, I get a blank page. The Chrome console shows the following error:
GET http://localhost:56074/crisis-center 404 (Not Found) Navigated to http://localhost:56074/crisis-center
and the Network trace clearly shows a 404 for crisis-center. In fact, if I use the navigation link on the main page for Crisis Center and click on it to show the crisis center component view, and then simply hit the refresh button to refresh the page while at the crisis center view, the same result happens.
Is this a Visual Studio issue? IIS Express? Other ideas?
We are a .Net development shop and our primary development environment is Visual Studio. If this is an issue with developing an Angular 2 application in Visual Studio using IIS Express, this may be a show stopper.
If anyone wants to try the same thing I can zip up my VS solution and share.
Anyone tried an Angular 2 application in Visual Studio using IIS Express that can maybe tell me what I am doing wrong?
Angular provides extensive set of navigation feature to accommodate simple scenario to complex scenario. The process of defining navigation element and the corresponding view is called Routing. Angular provides a separate module, RouterModule to set up the navigation in the Angular application.
It has two methods, navigate and navigateByUrl , that navigate the routes. They are similar; the only difference is that the navigate method takes an array that joins together and works as the URL, and the navigateByUrl method takes an absolute path.
Routing in Angular allows the users to create a single-page application with multiple views and allows navigation between them. Users can switch between these views without losing the application state and properties.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
Angular 2 by default uses HTML5 routing, you either have to map all server requests to index.html by adding the following to web.config
<rewrite>
<rules>
<rule name="redirect all" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="PATH TO INDEX.HTML" appendQueryString="true" />
</rule>
</rules>
</rewrite>
or implement the HashLocationStrategy, as described in angular docs here
provide(LocationStrategy, {useClass: HashLocationStrategy})
In addition to @stasivash answer:
If you have trouble making ajax requests to a certain path in your project, such as YOUR-DOMAIN/api/...
, I suggest adding the following condition:
<add input="{REQUEST_URI}" negate="true" pattern="^\/api\/.*$" ignoreCase="true" />
resulting in:
<rewrite>
<rules>
<rule name="redirect all" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
<add input="{REQUEST_URI}" negate="true" pattern="^\/api\/.*$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="/index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
For this issue, you need to use PathLocationStrategy or HashLocationStrategy for your application. It's available in the 'angular2/router'.
Example for using HashLocationStrategy:
boot.ts
bootstrap(AppCmp, [
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})
]);
and in your main component,
class AppCmp {
constructor(location: Location) {
location.go(location.path());
}
The location.path() dynamically gives the path for the page to be loaded.
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