Search code examples
angularangular-routingangular-router

Run angular 7 routing-based app as `file://` (without server)


I have angular app that is based on routing, what I want is to bundle it into WebView application as html file so it will be accessible by ~/app_path/index.html

I successfully built it with ng build --prod --output-path ./../ng-build and it loads fine in browser.

The problem is if I use router module I will get Unhandled Navigation Error warning and routes just don't work.

I tried to use hashes with useHash

imports: [RouterModule.forRoot(routes, {useHash: true})],

and expected it might work with routes e.g.

`~/app_path/index.html#settings`
`~/app_path/index.html#profile`
`~/app_path/index.html#details`

but it doesn't help - router crashes immediately after app launch

enter image description here

Is there any solution or workaround to achieve routing in this file:// mode? Or maybe another architecture approach to use in the app here instead of routing.


Solution

  • as per this thread setting <base> href attr to absolute path helps to solve routing issue

    <script>document.write('<base href="' + document.location + '" />');</script>
    

    so the base element will look like this

    <base href="file:///Users/guest/build/index.html">
    

    routing demo:

    enter image description here