且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有服务器的情况下在 file:///本地运行 Angular 7 项目

更新时间:2022-11-03 18:19:00

您可以双击 index.html 文件运行 angular 应用程序.只需在 app.module.ts 中添加以下代码

You can run angular app on double click on index.html file. Just add below code in your app.module.ts

注意:从 index.html 文件中删除 baseUrl = ./

note that : remove baseUrl = ./ from index.html file

//in App.module.ts : 

//import these packages  

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


// add these packages into providers as below : 

@NgModule({
    imports: 
     [
      .....
     ],
    declarations: 
     [
     ....
     ],
    providers: 
      [
        ....
        { provide: APP_BASE_HREF, useValue: '/' },
        { provide: LocationStrategy, useClass: HashLocationStrategy },
        
        ....
       ]
   ....
   
   })
   
   export class Appmodule{}

现在执行:npm run build 并双击 dist 文件夹中的 index.html 文件.您的应用应该可以运行.

Now execute : npm run build and double click the index.html file from dist folder. You app should run.