素材牛VIP会员
angular4.x ng serve运行没有问题但是打包后运行说没有匹配的路由?
 38***38  分类:JavaScript  人气:1238  回帖:1  发布于6年前 收藏

我是以这个命令“ng build --prod --aot --base-href ./”打的包!

app-routing.module.ts文件

import { ExtraOptions, RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';

const routes: Routes = [
    { path: '', loadChildren: 'app/home/home.module#HomeModule', pathMatch: 'full'}
];

const config: ExtraOptions = {
    useHash: false,
};

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

home-routing.module.ts文件

import {Routes, RouterModule} from '@angular/router';
import {HomeComponent} from './home.component';
import {NgModule} from '@angular/core';
import {HomeIndexComponent} from './home-index/home-index.component';

const routes: Routes = [
    {
        path: '',
        component: HomeComponent,
        pathMatch: 'full',
        children: [{
            path: '',
            component: HomeIndexComponent,
            pathMatch: 'full'
        }]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
    providers: []

})
export class HomeRoutingModule { }

app.module.ts文件

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpModule } from '@angular/http';
import { APP_BASE_HREF } from '@angular/common';

@NgModule({
  declarations: [
      AppComponent,
  ],
  imports: [
      BrowserModule,
      BrowserAnimationsModule,
      HttpModule,
      AppRoutingModule,
  ],
  providers: [
      { provide: APP_BASE_HREF, useValue: '/' }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

home.module.ts文件

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HomeRoutingModule} from './home-routing.module';
import {HomeComponent} from './home.component';
import {HomeIndexComponent} from './home-index/home-index.component';
import {HomeHeaderComponent} from './home-header/home-header.component';
import { ElModule } from 'element-angular';
import {HomeFooterComponent} from './home-footer/home-footer.component';

const HOME_COMPONENTS = [
    HomeComponent,
    HomeHeaderComponent,
    HomeIndexComponent,
    HomeFooterComponent
];

@NgModule({
  imports: [
    CommonModule,
    HomeRoutingModule,
    ElModule.forRoot()
  ],
  declarations: [
      ...HOME_COMPONENTS
  ]
})
export class HomeModule { }

请求各位大佬帮忙!网上查了重定向的方法并不好使,难道每一个path: ''都要重定向么?

 标签:javascript

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv1 新人
Al***ay 职业无 6年前#1

我已解决,但是要用redirect和hash,不然找不到页面刷新丢失页面!

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取