angular 嵌套路由

桃扇骨 2022-06-01 07:00 448阅读 0赞

mylogin.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'mylogin',
  4. template: `
  5. <h1>登录页</h1>
  6. <a routerLink="/mail">查看邮件</a>
  7. `
  8. })
  9. export class MyLoginComponent implements OnInit {
  10. constructor() { }
  11. ngOnInit() { }
  12. }

mail.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'mail',
  4. template: `
  5. <h1>邮箱主页面</h1>
  6. <a routerLink="/mail/outbox">发件箱</a>
  7. <router-outlet></router-outlet>
  8. `
  9. })
  10. export class MailComponent implements OnInit {
  11. constructor() { }
  12. ngOnInit() { }
  13. }

inbox.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'inbox',
  4. template: `
  5. <h1>收件箱</h1>
  6. <ul>
  7. <li>已读邮件1</li>
  8. <li>已读邮件2</li>
  9. <li>已读邮件3</li>
  10. </ul>
  11. `
  12. })
  13. export class InboxComponent implements OnInit {
  14. constructor() { }
  15. ngOnInit() { }
  16. }

outbox.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'outbox',
  4. template: `
  5. <h1>发件箱</h1>
  6. <ul>
  7. <li>已发邮件1</li>
  8. <li>已发邮件2</li>
  9. <li>已发邮件3</li>
  10. </ul>
  11. `
  12. })
  13. export class OutboxComponent implements OnInit {
  14. constructor() { }
  15. ngOnInit() { }
  16. }

app.router.ts

  1. {
  2. path: 'mail',
  3. component: MailComponent,
  4. children:[
  5. {path:'',component:InboxComponent},
  6. {path:'outbox',component:OutboxComponent},
  7. {path:'**',component:NotFoundComponent}
  8. ]
  9. },

发表评论

表情:
评论列表 (有 0 条评论,448人围观)

还没有评论,来说两句吧...

相关阅读