Skip to content

Refactor router recognition and redirects#5352

Closed
btford wants to merge 3 commits into
angular:masterfrom
btford:refactor-router-recognition-and-redirects
Closed

Refactor router recognition and redirects#5352
btford wants to merge 3 commits into
angular:masterfrom
btford:refactor-router-recognition-and-redirects

Conversation

@btford
Copy link
Copy Markdown
Contributor

@btford btford commented Nov 17, 2015

This has a little bit more cleanup work, but I'd like @matsko to start reviewing the code ASAP so we can finally land this. 🐈


This is a big change. @matsko also deserves much of the credit for the implementation.

Previously, ComponentInstructions held all the state for async components.
Now, we introduce several subclasses for Instruction to describe each type of navigation.

BREAKING CHANGE:

Redirects now use the Link DSL syntax. Before:

@RouteConfig([
  { path: '/foo', redirectTo: '/bar' },
  { path: '/bar', component: BarCmp }
])

After:

@RouteConfig([
  { path: '/foo', redirectTo: ['Bar'] },
  { path: '/bar', component: BarCmp, name: 'Bar' }
])

BREAKING CHANGE:

This also introduces useAsDefault in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.

Previously, you could use redirectTo like this to expand a URL like /tab to /tab/posts:

@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }

Now the recommended way to handle this is case is to use useAsDefault like so:

@RouteConfig([
  { path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }

@RouteConfig([
  { path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
  { path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }

In the above example, you can write just ['/Tab'] and the route Posts is automatically selected as a child route.

Closes #4170
Closes #4490
Closes #4694
Closes #5200

@btford btford added comp: router action: review The PR is still awaiting reviews from at least one requested reviewer effort2: days refactoring Issue that involves refactoring or code-cleanup labels Nov 17, 2015
@btford btford added this to the beta-00 milestone Nov 17, 2015
@tandu
Copy link
Copy Markdown

tandu commented Nov 17, 2015

I think you have a mistype - by default should be selected route Posts.

Can you make it so that if component is not defined it doesn't add anything to router-outlet? I.e. for this route

{ path: '/posts' useAsDefault: true}

TabsCmp is loaded but router-outlet is ignored or default stub component is autogenerated and added. I had to define stub component because for default route I don't need anything to be shown in router-outlet.

@tandu
Copy link
Copy Markdown

tandu commented Nov 17, 2015

To explain. I have home component. It is possible to run gallery from the home that way home/gallery/111. So if url is home/ router-outlet is not required, it is only needed to show gallery when it is activated from home.

@btford btford force-pushed the refactor-router-recognition-and-redirects branch from 161eeeb to 922f2d4 Compare November 17, 2015 21:54
@btford
Copy link
Copy Markdown
Contributor Author

btford commented Nov 18, 2015

I think "routes with an empty component" is a separate (but reasonable) concern. Right now you can easily construct a component with blank template and reference it where you need. @tandu could you file an issue describing this?

@tandu
Copy link
Copy Markdown

tandu commented Nov 18, 2015

Here are other issues related to router
#5359
#5345

@btford
Copy link
Copy Markdown
Contributor Author

btford commented Nov 18, 2015

Great! Thanks for the write-up, @tandu.

@btford btford force-pushed the refactor-router-recognition-and-redirects branch from 922f2d4 to 158152e Compare November 18, 2015 23:02
@LMFinney
Copy link
Copy Markdown

@btford Would it be possible to have a version in which I could programmatically decide what to do when the child route is left off?

In our app, we have a tabbed interface that I want to correspond to child routes. However, the contents of the tabs depend on the data I've loaded with these possibilities:

ID
AST 1
AST 1, ID
AST Summary, AST 1, AST 2
AST Summary, AST 1, AST 2, ID

I would like to have a child route for the selected tab to be able to specify via URL which tab is selected, but I also want the first tab to be selected by default if none is specified in the URL. In all the examples I've seen, I need to declare in code which one is to be used as default, but in my case any of three different tabs could be the one to use depending on the data.

Is this problem solvable through AsyncRoutes combined with Child Routes somehow?

@btford btford force-pushed the refactor-router-recognition-and-redirects branch 5 times, most recently from a13a677 to 1467f2e Compare November 20, 2015 21:43
This is a big change. @matsko also deserves much of the credit for the implementation.

Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.

BREAKING CHANGE:

Redirects now use the Link DSL syntax. Before:

```
@RouteConfig([
  { path: '/foo', redirectTo: '/bar' },
  { path: '/bar', component: BarCmp }
])
```

After:

```
@RouteConfig([
  { path: '/foo', redirectTo: ['Bar'] },
  { path: '/bar', component: BarCmp, name: 'Bar' }
])
```

BREAKING CHANGE:

This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.

Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:

@RouteConfig([
  { path: '/tab', redirectTo: '/tab/users' }
  { path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }

Now the recommended way to handle this is case is to use `useAsDefault` like so:

```
@RouteConfig([
  { path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }

@RouteConfig([
  { path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
  { path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```

In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.

Closes angular#4170
Closes angular#4490
Closes angular#4694
Closes angular#5200
@btford btford force-pushed the refactor-router-recognition-and-redirects branch from 1467f2e to ab8645c Compare November 20, 2015 22:17
@btford
Copy link
Copy Markdown
Contributor Author

btford commented Nov 20, 2015

@matsko LGTM'd this

@btford btford added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 20, 2015
@mary-poppins
Copy link
Copy Markdown

Merging PR #5352 on behalf of @alxhub to branch presubmit-alxhub-pr-5352.

@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.