将基于angular 7路由的应用程序作为`file://`运行(不带服务器) [英] Run angular 7 routing-based app as `file://` (without server)

查看:132
本文介绍了将基于angular 7路由的应用程序作为`file://`运行(不带服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于路由的角度应用程序,我想要将其作为html文件捆绑到WebView应用程序中,以便~/app_path/index.html

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

我使用ng build --prod --output-path ./../ng-build成功构建了它,并且在浏览器中可以正常加载.

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

问题是,如果我使用路由器模块,则会收到Unhandled Navigation Error警告,并且路由不起作用.

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

我尝试对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

在此file://模式下是否有任何解决方案或解决方法来实现路由?或者也许是在此处的应用中使用的另一种架构方法,而不是路由.

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.

推荐答案

按照此线程<base> href属性设置为绝对路径有助于解决路由问题

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">

路由演示:

这篇关于将基于angular 7路由的应用程序作为`file://`运行(不带服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆