我试图将angularjs 2 app dist文件作为简单的html页面运行 . 即我只需双击dist文件夹中的index.html . 但该应用程序无法正常工作:错误:
EXCEPTION:Uncaught(承诺):SecurityError:无法在'History'上执行'replaceState':无法在原点为'null'的文档中创建URL为'file:/// D:/'的历史状态对象URL'file:/// D:/dev/ProxyVoting/ngapp/dist/index.html' .
6 回答
使用 HTTP server instead of direct file access . 我遇到了同样的问题;所以,我使用XAMPP并使其正常工作 .
所以,你的 index.html 将包含
base href
这样的东西 -<base href="http://localhost:81/etl/">
使用HTTP协议而不是文件传输协议 . 对于http服务器中的那个开放构建 . 我正在使用节点服务器
为什么不是那样的?
First Step:
运行命令
要么
Second Step:
更改dist目录中的index.html
Third Step:
将所有文件放入服务器(可能是localhost或任何服务器中的htdoc)
希望它能奏效 .
我有同样的问题,我完全在一个相当复杂的角度2应用程序与多个嵌套路线工作 .
我所需要的只是
使用
CommonModule,RouterModule.forRoot(routes,{useHash:true})
进行路由器配置当然你需要首先导入CommonModule
import { CommonModule } from '@angular/common';
这在index.html文件中从html中删除了基础Href = "/"标签,并像这样添加了它 .
<script>document.write('<base href="' + document.location + '" />');</script>
在实际的Web服务器上运行
dist
文件夹解决了我的问题 .