I'm creating a Dashboard like application. I'd like to achieve the following layout plan in Angular (2+):
- route - name - layout
- / - home page - full width layout with tables and charts, etc
- /reports - reports page - same full width layout with more tables, etc
- /login - login page - no full width layout, just a simple login form at screen center
- /signup - signup page - no full width layout, just a simple signup form at screen center
- /messages - emails - full width layout
- /messages/new - new email - medium layout, not inheriting from full width layout
etc...
So basically what I'd like to do is to completely replace the contents of <body>
at some (child) routes.
This is not good for me: multiple layout for different pages in angular 2 because I don't want to redirect / (root) to anywhere like /home.
This one doesn't fit either: How to switch layouts in Angular2
Any help would be great!
Ok I am going to give this a shot...
Routes
Creating routes can be done in multiple ways. You can use child routes or serve the component up directly.
If you want to serve the component up directly this would be ideal,
Direct problems you are facing
Three problems,
Show a component as a child.
Show a component in a template called fullwidth
Show a component in a template called mediumwidth
In your routes.ts
This is going to forward all paths in the URL to look to these routes. It will check the routes to see which template it will fall in.
Then create 3 directories.
/blank
/full
/medium
Inside these folders you will keep your component that use each of the mother templates.
So since login is blank. It would be in /blank
/blank/BlankComonent.ts
Also in each of these directories you will create a routes file which is referred to in the initial routes file we have already created.
/blank/blank.routes.ts
Then in medium the same thing,
/blank/blank.routes.ts
And then the same for
FULL_ROUTES
Make a routes file for each directory we created. Add all your routes that live in the same directory and will share the same mother template.
Then we will create a templates directory. Call it /layouts
Now in that direcotry this is where you will create
BlankComponent.ts FullComponent.ts MediumComponent.ts
Each of these components will have their corresponding routes served inside of these templates. Because remember our first
routes
file says that we will serve allChild Routes
to thesetemplates
.So the layouts will be served to the
router-outlet
You can solve your problem using child routes.
See working demo at https://angular-multi-layout-example.stackblitz.io/ or edit at https://stackblitz.com/edit/angular-multi-layout-example
Set your route like below
Recommended reference: http://www.tech-coder.com/2017/01/multiple-master-pages-in-angular2-and.html