在没有服务器的情况下在 file:///本地运行 Angular 7 项目 [英] Run Angular 7 project locally on file:/// without server
问题描述
我想构建我的 angular 项目并生成一个包含它的 ZIP 文件以通过电子邮件发送它,我希望收到它的人能够在他的桌面上点击 index.html 文件打开它.
I want to build my angular project and generate a ZIP file containing it to send it via email and I want the person who receives it to be able to open it on his Desktop clicking index.html file.
我将 baseUrl 更改为 ./或 document.location,但出现以下错误:未处理的导航错误"
I changed the baseUrl to ./ or to document.location but I'm getting the following error: "Unhandled Navigation Error"
有人知道如何解决这个问题吗?
Does anyone have any hint about how to fix this?
推荐答案
您可以双击 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.
这篇关于在没有服务器的情况下在 file:///本地运行 Angular 7 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!