【angular路由】路由懒加载写法

路由懒加载一直没明白怎么加载的,现在记录一下

要实现的功能

在这里插入图片描述
组件:
home组件下有setting和welcome组件,默认进入home组件显示welcome组件的内容
product组件下有pcate和plist组件
在这里插入图片描述
app.component.html

<header class="header">
  <a [routerLink]="[ '/home']" routerLinkActive="active">首页</a>
  <a [routerLink]="[ '/product']" routerLinkActive="active">商品</a>
  <a [routerLink]="[ '/news']" routerLinkActive="active">新闻</a>
</header>
<router-outlet></router-outlet>
<!--
  单页面应用的好处:  运行速度快
  
  坏处:  一个页面不利于seo优化
-->

app.routing-module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
// 这里不用引入组件
const routes: Routes = [
  {
    path: 'home',
    // 使用() => import('./home/home.module').then(m => m.HomeModule) ng build --prod打包后会报错:Runtime compiler is not loaded
    // loadChildren: () => import('./home/home.module').then(m => m.HomeModule),
    loadChildren: './home/home.module#HomeModule'
  }, {
    path: 'product',
    // loadChildren: () => import('./product/product.module').then(m => m.ProductModule),
    loadChildren: './product/product.module#ProductModule'
  }, {
    path: 'news',
    // loadChildren: () => import('./news/news.module').then(m => m.NewsModule),
    loadChildren: './news/news.module#NewsModule'
  }, {
    // 匹配不到路由的时候加载的组件或者跳转的地址
    path: '**',
    redirectTo: 'home'
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RouterModule } from '@angular/router';
// 这里不需要引入homeModule、ProductModule、NewsModule,点击路由的时候才加载这些模块,所以叫懒加载

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

home.component.html

<div class="content">
  <div class="left">
    <!--
      路由写法:[routerLink]="['/home/welcome']" === [routerLink]="['welcome']"
    -->
    <a [routerLink]="['welcome']" routerLinkActive="active">欢迎首页</a>
    <br>
    <br>
    <a [routerLink]="['/home/setting']" routerLinkActive="active">系统设置</a>
  </div>
  <div class="right">
    <router-outlet></router-outlet>
  </div>
</div>

home.component.css

.header  {
 height: 40px;
 background: black;
}
.header  a {
    padding: 0 50px;
    display: inline-block;
    color: #fff;
}
.header .active {
    color: red;
}

home下的index.ts
慕课网教程的写法,使用index.ts导出,引用方法为import { SettingComponent } from ‘./setting/index’;

export * from './home.module'
export * from './setting'
export * from './welcome'

home/setting/index.ts

export * from './setting.component'

home/welcome/index.ts

export * from './welcome.component'

home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';
// module文件必须引入路由模块
import { HomeRoutingModule } from './home-routing.module';
// module文件需要引入所有的commponent
import { SettingComponent } from './setting/index';
import { WelcomeComponent} from './welcome/index';

@NgModule({
    declarations: [
        // 用到的组件需要声明
        HomeComponent,
        SettingComponent,
        WelcomeComponent
    ],
    imports: [CommonModule, RouterModule, HomeRoutingModule],
    exports: [],
    providers: [],
})
export class HomeModule { }

home.routing-module.ts

import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { HomeComponent } from './home.component';
import { SettingComponent } from './setting';
import { WelcomeComponent } from './welcome';

const routes: Routes = [
    {
        // 默认路由
        path: '',
        component: HomeComponent,
        children: [{
            path: '',
            component: WelcomeComponent,
        },{
            path: 'welcome',
            component: WelcomeComponent,
        }, {
            path: 'setting',
            component: SettingComponent
        }, ]
    },
    // {
    //     // 这样写就相当于把WelcomeComponent这个组件挂载到根路由app-routing.module
    //     path: 'welcome',
    //     component: WelcomeComponent
    // }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class HomeRoutingModule { }

当在浏览器输入http://localhost:4200/welcome就会跳转到welcome组件,相当于从根路由跳转,前提app.module.ts不写path: ‘**’,redirectTo: ‘home’
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

我出错的几个地方:

  1. app.routing.module.ts中懒加载不需要引入组件
  2. app.module.ts中不需要引入首页、产品、新闻模块
  3. home.module.ts文件里需要引入home.routing.module.ts
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值