2016-12-29 53 views
2

我已经构建了一个使用angular2的网站,它在localhost上工作正常,但是当我将其部署在我的服务器上时,所有css和js链接都显示为中断。angular2中的路由不工作在活动服务器

ex:localhost:4200/home正常,但ip:4200/home为css和js文件提供404。

IP:4200本身重定向到回家,但键入IP:4200 /家给404甚至重装它给404

我想我需要修复这些路由。

我的路由器TS文件如下:

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


import { HomeComponent } from './component/home.component'; 
import { RegisterComponent } from './component/register.component'; 
import { ProductComponent } from './component/product.component'; 

const appRoutes: Routes = [ 

    { 
    path: '', 
    redirectTo: '/home', 
    pathMatch: 'full' 
    }, 

    { 
    path: 'home', 
    component: HomeComponent 
    }, 
    { 
    path: 'register', 
    component: RegisterComponent 
    }, 
    { 
    path: 'product', 
    component: ProductComponent 
    } 


]; 

出口const的路由:ModuleWithProviders = RouterModule.forRoot(appRoutes);

和app.module.ts是这样的:

import { NavBar } from './component/nav-bar.component'; 
import { RegisterService } from './services/register.service'; 
import { HomeService } from './services/home.service'; 
import { ProductService } from './services/product.service'; 
import { ProductComponent } from './component/product.component'; 
import { NavBar1 } from './component/nav-bar1.component'; 
@NgModule({ 
    declarations: [ 
    AppComponent, 
    HomeComponent, 
    RegisterComponent, 
    NavBar, 
    ProductComponent, 
    NavBar1 

    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    RouterModule.forRoot([ 
     { 
      path: 'register', 
      component: RegisterComponent 
     } 
     ]), 

    routing 
    ], 
    providers: [ 
RegisterService, 
HomeService, 
ProductService 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

可能http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser –

回答

0

切换到HashLocationStrategy

providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}, ... 

或配置您的服务器以支持HTML5 pushState的(重写请求不存在的文件index.html

+0

provice是给n ode_modules/@ angular/core/index“'没有导出成员'provice'。 甚至我把它使用提供的尝试:。 其给予同样的错误 node_modules/@角/核心/指数“”没有出口成员‘提供’ – kirti

+0

对不起,这是一个错字 - 应该是'provide' –

+0

我曾尝试提供aswel,但仍给出相同的错误node_modules/@ angular/core/index“'没有导出成员'提供',我是否需要为此安装任何模块 – kirti

相关问题