尝试使用angular-cli服务器访问不同模块中的直接URL时出现404错误

Cel*_*gra 1 angular2-routing angular-cli angular

所以,我试图使用angular2直接访问url,使用angular-cli.所以,当我访问网址http://192.168.56.103:3000/app时,应用程序和所有模块及其路径都能正常工作.

但是当我试图直接访问模块路径时(例如http://192.168.56.103:3000/employee/upload),我收到了这个错误:

upload:10 GET http://192.168.56.103:3000/employee/styles.e7a71cdafac175e58c1d2b9d347ab895.bundle.css 
upload:18 GET http://192.168.56.103:3000/employee/inline.d41d8cd98f00b204e980.bundle.js 
upload:18 GET http://192.168.56.103:3000/employee/styles.d6983b9a92d269fc6b29.bundle.js 
upload:18 GET http://192.168.56.103:3000/employee/scripts.916e0bd9d793165463ce.bundle.js 
upload:18 GET http://192.168.56.103:3000/employee/main.aa7bb73e6fe0d8821716.bundle.js 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

它是这样的,Href Base不是http://192.168.56.103:3000/ root,但它是http://192.168.56.103:3000/employee.因此,我的应用程序遇到了一些错误,找不到这些文件.那么如何使用angular-cli服务器解决angular2中的问题呢?

此外,我试图在我的模块中添加一个基本Href,但没有任何反应,错误仍然相同:

import { CommonModule, APP_BASE_HREF }   from '@angular/common';

@NgModule({
  imports: [
  ],
  declarations: [

  ],
  providers: [
     ....
    {provide: APP_BASE_HREF, useValue : '/' }
  ]
})
...
Run Code Online (Sandbox Code Playgroud)

我的路由如下所示:

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

const entity : string = 'employee';

const employeeRoutes: Routes = [
    { path: entity,
        children: [
        { path: '', redirectTo: 'list', canActivate: [AuthGuard] },
        { path: 'list', component: ListComponent, canActivate: [AuthGuard] },
        { path: 'upload', component: UploadComponent, canActivate: [AuthGuard] },
        { path: 'edit/:id', component: EditComponent, canActivate: [AuthGuard] }
        ]
    }
];

export const employeeRouting: ModuleWithProviders = RouterModule.forChild(employeeRoutes);
Run Code Online (Sandbox Code Playgroud)

需要一些帮助.谢谢!

Cel*_*gra 6

问题解决了!

我只是删除APP_BASE_HREF并在主模块中添加了位置策略:

// ...
import { HashLocationStrategy, LocationStrategy } from '@angular/common';

@NgModule({
  imports: [
    // ...
  ],
  declarations: [
    // ...
  ],
  providers: [
    // ...
    {provide: LocationStrategy, useClass: HashLocationStrategy}
  ],
  bootstrap: [ AppComponent ]
})

export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)

现在我可以直接访问嵌套的URL.