fix(generate): stop default browser error from ng new --routing #2794
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes the following issue with
ng new
:Problem
If you run:
In the browser you see an error (Error log follows at the end). The error happens in the browser console, and it's a symptom of the route matching not finding any routes to match.
The app is initialized with an empty routes array, so when the routing module tries to use the routes array to match the URL
http://localhost:4200/
, it gets nothing, because the array is empty.Makes sense, but it's still not a good 1st time experience though, to throw error by default, when all the user did was creating a new project, relying on the CLI for good defaults.
Solution
The correct way to fix this is to have another component and setup the routing to show it inside the
AppComponent
.This leads to several decisions, like what to name the component, etc. which the CLI would rather not be involved in.
So, this PR puts the minimalist code that can make the browser NOT show errors, leave a good start for users to add their own routes, and doesn't make any special decisions.
Notes
Tests
Since tests seem to only check for files being present - and not check for their content, no tests were added or modified.
AoT
This PR is different from #2680. The error this PR fixes is NOT AoT specific at all. It's running plain
ng serve
after creating default project structure with no modifications.Hence, it's also different from the problem described in #2680. Both are issues that happen in the browser console, but the problem this PR addresses happens with the user NOT adding any routes (or making any project modifications at all after using
ng new --routing
), and it happens without AoT.The problem in #2680 happens after the user adds syntactically correct routes, and only happens when serving with
--aot
.Error Details
This is the specific error that shows in the browser following the steps mentioned above: