【问题标题】:Routing not working on Angular 4路由不适用于Angular 4
【发布时间】:2018-02-18 14:01:40
【问题描述】:

主页工作正常,但是当我在应用程序中访问其他路由时会抛出错误。

app.module.ts

import { environment } from './../environments/environment';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import {HttpModule} from '@angular/http';
import {RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { NavheaderComponent } from './navheader/navheader.component';
 import { SwiperModule, SwiperConfigInterface } from 'ngx-swiper-wrapper';
import { SliderComponent } from './slider/slider.component';
import {HomeService} from './services/banner/home.service';
import { FeaturedProductComponent } from './featured-product/featured-product.component';
import { CurrencyChangePipe } from './pipe/currency-change.pipe';
import { FooterComponent } from './footer/footer.component';
import { CategoriesComponent } from './categories/categories.component';
import { ProductPageComponent } from './product-page/product-page.component';
  
const SWIPER_CONFIG: SwiperConfigInterface = {
  direction: 'horizontal',
  threshold: 50,
  spaceBetween: 5,
  slidesPerView: 1,
  centeredSlides: true,
  keyboardControl: true
};
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    NavheaderComponent ,
    SliderComponent, 
    FeaturedProductComponent, 
    CurrencyChangePipe, 
    FooterComponent, 
    CategoriesComponent, 
    ProductPageComponent   
  ],
  imports: [
    BrowserModule,HttpModule,
    RouterModule.forRoot([
    {path:'', component: HomeComponent  },
    {path:'product', component: ProductPageComponent }
    ]),  
    SwiperModule.forRoot(SWIPER_CONFIG) 
 
  ],
  providers: [  {
            provide: APP_BASE_HREF,
            useValue: '/' + (window.location.pathname.split('/')[1] || '')
        },
        HomeService 
        ],
  bootstrap: [AppComponent]
})
export class AppModule { }

以下是控制台中的错误。

GET http://localhost:4200/product/assets/css/style.min.css net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/polyfills.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/inline.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/styles.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/scripts.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/vendor.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/main.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/polyfills.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/styles.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/scripts.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/vendor.bundle.js net::ERR_ABORTED
(index):31 GET http://localhost:4200/product/main.bundle.js net::ERR_ABORTED

localhost:4200 工作正常,但是 localhost:4200/product 没有。似乎产品组件没有加载。这里所有的 url 都是错误的,因为它们没有找到。 正确的应该是这样的。

http://localhost:4200/scripts.bundle.js 

【问题讨论】:

  • 你在 index.html 中添加了<base href="/"> 吗?

标签: javascript angular


【解决方案1】:

尝试使用 useHash

RouterModule.forRoot([
{path:'', component: HomeComponent  },
{path:'product', component: ProductPageComponent }
], { useHash: true })

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多