1

I tried to run angularjs 2 app dist files as simple html page. i.e. I just double click index.html in the dist folder. But the app is not working: Error:

EXCEPTION: Uncaught (in promise): SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///D:/' cannot be created in a document with origin 'null' and URL 'file:///D:/dev/ProxyVoting/ngapp/dist/index.html'.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Bhaskar Bs
  • 21
  • 1
  • 2
  • 1
    You can't just click on index.html and except it to work. Your page needs some mandatory files that are not accessible without using a server (Apache, node.js, ...). – mickdev Jan 31 '17 at 08:11
  • @mickdev can you be descriptive about what kind of files the page needs? Thanks. – Harsh Vakharia Feb 02 '17 at 09:56
  • See here for more detail about Angular 2 App production : http://stackoverflow.com/a/38765929/7447071 – mickdev Feb 02 '17 at 16:05

6 Answers6

2

First Step:

Run the command

ng build

or

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

Second Step:

Change at index.html in dist directory

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

Third Step:

Put all files into server(may be htdocs in localhost or any server)

Hopefully it will work.

sabuz
  • 829
  • 8
  • 10
1

use an HTTP server instead of direct file access. I was facing the same problem; So, I used XAMPP & got it working.

So, your index.html would contain base href something like this - <base href="http://localhost:81/etl/">

Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
1

Use HTTP protocol instead of file transfer protocol. For that open build in http server. I'm using node server

npm install http-server -g

cd path/folder

http-server
Vega
  • 27,856
  • 27
  • 95
  • 103
Antajo Paulson
  • 555
  • 1
  • 4
  • 15
1

why not something like that?

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

I had the same problem and I got it working completely in a fairly complex angular 2 app with multiple nested routes.

All I needed was

Router configuration with CommonModule,RouterModule.forRoot(routes,{useHash:true})

Of course you need to import CommonModule first import { CommonModule } from '@angular/common';

and this in the index.html file Removed base Href="/" tag from html and added it like this. <script>document.write('<base href="' + document.location + '" />');</script>

Amr Ammar
  • 43
  • 2
0

Running the dist folder on actual web server solved my issue.

Omar
  • 111
  • 1
  • 3