首页 文章

angularjs 2:SecurityError:执行失败

提问于
浏览
1

我试图将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 回答

  • 1

    使用 HTTP server instead of direct file access . 我遇到了同样的问题;所以,我使用XAMPP并使其正常工作 .

    所以,你的 index.html 将包含 base href 这样的东西 - <base href="http://localhost:81/etl/">

  • 1

    使用HTTP协议而不是文件传输协议 . 对于http服务器中的那个开放构建 . 我正在使用节点服务器

    npm install http-server -g
    
    cd path/folder
    
    http-server
    
  • 0

    为什么不是那样的?

    history.replaceState = (data:any, title:string, url?:string) => {;//nothing};
    
  • 2

    First Step:

    运行命令

    ng build
    

    要么

    ng build -prod (then it will compact all files for production version)
    

    Second Step:

    更改dist目录中的index.html

    <base href="/"> to <base href="./">
    

    Third Step:

    将所有文件放入服务器(可能是localhost或任何服务器中的htdoc)

    希望它能奏效 .

  • 0

    我有同样的问题,我完全在一个相当复杂的角度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>

  • 0

    在实际的Web服务器上运行 dist 文件夹解决了我的问题 .

相关问题