简体   繁体   English

导出Angular单页应用到Static HTML/CSS/JS

[英]Export Angular single page Application to Static HTML/CSS/JS

I am looking for way to convert single page Angular Application to old style html-css-javascript static files, which runs on my pc without any set-up of complicated node.js or any npm I am looking for way to convert single page Angular Application to old style html-css-javascript static files, which runs on my pc without any set-up of complicated node.js or any npm

What I was looking for and came to Angular is, I was heavily looking for Bootstrap-sidebar with lightweight css and js but all the examples I could find had large, big size of Javascript files, so I saw Angular had some novel implementation of Sidebar. What I was looking for and came to Angular is, I was heavily looking for Bootstrap-sidebar with lightweight css and js but all the examples I could find had large, big size of Javascript files, so I saw Angular had some novel implementation of Sidebar .

I want to convert the Angular sidebar(Angular application) to old style HTML , css and js files so that I can run them without any complex node server.我想将 Angular 侧边栏(Angular 应用程序)转换为旧式HTMLcssjs文件,这样我就可以在没有任何复杂节点服务器的情况下运行它们。

Is there any way to get the javascript, css and html out of Angular application.有什么方法可以让 javascript、css 和 html 脱离 ZC31C335EF37283C451B18BA0DD371 应用程序。 I heard of WebPack but did not tried, so any suggestion would be appreciated.我听说过WebPack但没有尝试过,所以任何建议都将不胜感激。

If the above process can be done in React suggest me a solid way to do that.如果上述过程可以在React中完成,请建议我一个可靠的方法来做到这一点。

Edit: What I want is, I want to access the file as file://path/to/index.html I do not want HTTP ( http://localhost/index.html ) Edit: What I want is, I want to access the file as file://path/to/index.html I do not want HTTP ( http://localhost/index.html )

Note笔记

When compiling an angular project, the result is already a plain html/css/js website.在编译一个angular项目时,结果已经是一个普通的html/css/js网站了。 You do not need nodejs to run angular, but, like for any website, you should use a http server.您不需要 nodejs 来运行 angular,但是,与任何网站一样,您应该使用 http 服务器。 So your problem is rather that you cannot open an angular website from a file, without a webserver.因此,您的问题在于,如果没有网络服务器,您无法从文件中打开 angular 网站。

Explanation解释

Your problem is likely that you have errors in the console when opening the resulting index.html from your browser.您的问题可能是从浏览器打开生成的index.html时控制台出现错误。 They come from improved security stadards implemented by browsers (CORS, Strict mime type, access to local files,...).它们来自浏览器实现的改进的安全标准(CORS、严格的 mime 类型、访问本地文件……)。 Since you did not provide any error details at all apart from stating that you only have a blank page, here is a basic solution由于除了说明您只有一个空白页面之外,您根本没有提供任何错误详细信息,因此这是一个基本解决方案

Solution解决方案

Try setting base href to .尝试将 base href 设置为. when building建的时候

ng build --prod --base-href=.

The website should work without further action on Firefox.该网站无需对 Firefox 采取进一步操作即可正常工作。

On chrome, you may get one of the following errors在 chrome 上,您可能会遇到以下错误之一

Access to script at 'file://XXXXXXscripts.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. CORS 策略已阻止从源“null”访问“file://XXXXXXscripts.js”处的脚本:仅协议方案支持跨源请求:http、data、chrome、chrome-extension、Z5E05617.BADEA6

Or或者

Failed to load module script: The server responded with a non-JavaScript MIME type of "".加载模块脚本失败:服务器以非 JavaScript MIME 类型“”响应。 Strict MIME type checking is enforced for module scripts per HTML spec根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查

A possible solution is to remove the type="module" attribute from all script tags at the bottom of index.html if using angular 8+.如果使用 angular 8+,则可能的解决方案是从index.html底部的所有script标签中删除type="module"属性。

There are other options to solve CORS related issues on local files, but it involves disabling security on chrome, which is not a good idea.还有其他选项可以解决本地文件上与 CORS 相关的问题,但它涉及禁用 chrome 上的安全性,这不是一个好主意。

It's really simple You can run npm run build / ng build command.真的很简单你可以运行 npm run build / ng build 命令。 The build process will transpile all the code into equivalent js code and you can find this transpiled code in "dist" folder of your root project directory.构建过程会将所有代码转换为等效的 js 代码,您可以在根项目目录的“dist”文件夹中找到此转换后的代码。 You can use see there index.html which will be your base html page and other js files or media files which are javascript files.您可以使用查看 index.html 这将是您的基本 html 页面和其他 js 文件或媒体文件,这些文件是 javascript 文件。 You can use this content in dist folder to host your website which will be purely html css and js based.您可以使用 dist 文件夹中的此内容来托管您的网站,该网站将纯粹基于 html css 和 js。 You can use nginx or any other server for hosting it.您可以使用 nginx 或任何其他服务器来托管它。

in Angular you can build your angular application using ng build .在 Angular 中,您可以使用ng build构建您的 angular 应用程序。 This will generate a pure js equivalent of the.ts code you wrote.这将生成与您编写的 .ts 代码等效的纯 js 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法将当前呈现的 angular 页面连同所有 JS 功能一起导出到 static HTML ? - Is there any way to export the current rendered angular page to static HTML along with all the JS functionalities? 从 Rails 导出静态 HTML+CSS+JS - Export static HTML+CSS+JS from Rails 在Angular 2应用和静态html页面之间共享的Material Design CSS / JS - Material Design CSS/JS shared between Angular 2 app and static html page PhoneGap应用程序是像Angular Js一样的单页应用程序模型吗? - Is PhoneGap Application is Single Page Application model like Angular Js? 如何在Rails应用程序中包含带有自定义css和js文件的静态页面 - How to include a static page with custom css and js files in a Rails application 将 HTML 页面导出到 angular 中的 pdf - Export HTML page to pdf in angular 数据绑定在单页应用程序(JS和HTML)中属于什么地方 - Where does data binding belong in a single page application (JS and HTML) Nuxt.js static 站点与使用 Vue.js 的动态单页应用程序? - Nuxt.js static site with a dynamic single page application using Vue.js? 如何计算角度JS应用程序的页面加载时间(单页面应用程序) - How to calculate the page load time for angular JS application( single page application) 在Angular 4应用程序中将HTML表导出到CSV? - export html table to csv in angular 4 application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM