在iis上发布时出现Angular 2错误

时间:2017-10-12 08:03:38

标签: angular iis

我已经在角度2中创建了演示应用程序,当我在本地运行它时工作正常,但是当我在iis服务上发布它时出错。

我正在犯这个错误

Failed to load resource: the server responded with a status of 404 (Not Found)

enter image description here

这是我的代码index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">
  <link rel="stylesheet" href="https://bootswatch.com/darkly/bootstrap.min.css">  
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script>
    System.import('app').catch(function(err){ console.error(err); });
  </script>
</head>
<body>
  <app-root>Loading...</app-root>
</body>

</html>

app.router.ts

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

//import {AppComponent} from './app.component'
import {AboutComponent} from './about/about.component'
import {TechnologyComponent} from './technology/technology.component'

export const router : Routes = [
    {path:'',redirectTo:'about',pathMatch:'full'},
    {path:'about',component:AboutComponent},
    {path:'tachnology',component:TechnologyComponent}
]
export const routes: ModuleWithProviders = RouterModule.forRoot(router)

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

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

import {routes} from './app.router'
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { TechnologyComponent } from './technology/technology.component';

import {AboutSrvice} from './about/about.service'
@NgModule({
  declarations: [
    AppComponent,
    AboutComponent,
    TechnologyComponent,
  ],

  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routes
  ],
  //There is two ways you can use service first here you can add service in provider or either you can add it in about.component.ts file in @Component({}) section 
 // providers: [AboutSrvice,{provide: LocationStrategy, useClass: HashLocationStrategy}],
 providers: [AboutSrvice],
  bootstrap: [AppComponent]
})
export class AppModule { }

我是否遗漏了某些内容或需要任何额外的内容才能发布?

3 个答案:

答案 0 :(得分:1)

好的问题是Angular使用base ref“/”创建所有内容。因此,它创建的index.html将包含 / filename 的文件。

您需要做的是删除基础参考并将其更改为<base href="">

然后查看您的webpack文件并在webpack.prod中删除。改变:

publicPath: '/',

分为:

publicPath: '',

现在,如果使用路线读取此https://angular.io/api/common/HashLocationStrategy并实施它。

修改

对于CLI而不是更改.prod webpack,您可以这样做:

ng build --prod --aot --no-sourcemap --base-href ""

答案 1 :(得分:1)

it terns out that it is a conflict in versions so

let us play :D

npm uninstall -g @angular/cli
npm cache clean

then after that

npm install -g @angular/cli@latest --save

create new project in a new folder

ng new TheProjectName

close the editor

copy src from the old project to the new project

run the editor

build and deploy

ng build --prod --aot --no-sourcemap --base-href

答案 2 :(得分:0)

我发生了同样的问题, 我刚刚在index.html

中进行了更改
<base href="/websitename/">