对于使用AngularJShtml5Mode的SpringBoot感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍angular+springboot,并为您提供关于Angular1位置html
对于使用AngularJS html5Mode的Spring Boot感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍angular+springboot,并为您提供关于Angular1位置html5mode等效于angular2、Angularjs $location html5mode浅析、AngularJS HTML5Mode、AngularJS html5mode 与第三方库冲突的有用信息。
本文目录一览:- 使用AngularJS html5Mode的Spring Boot(angular+springboot)
- Angular1位置html5mode等效于angular2
- Angularjs $location html5mode浅析
- AngularJS HTML5Mode
- AngularJS html5mode 与第三方库冲突
使用AngularJS html5Mode的Spring Boot(angular+springboot)
我使用Spring Boot启动Web应用程序。它使用一个简单的主类来启动嵌入式tomcat服务器:
@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
我想以他可以处理的angularjs html5mode方式配置服务器
$locationProvider.html5Mode(true);
其他用户的相关发布表明您需要重定向到根目录。在html5模式下,从网址中删除哈希包。如果刷新页面,服务器将找不到该页面,因为他不处理哈希。
答案1
小编典典我找到了可以接受的解决方案。
@Controllerpublic class ViewController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/app/**") public String app() { return "index"; }}
angularjs应用必须在子域应用下。如果您不希望这样做,则可以创建一个子域(例如app.subdomain.com),该子域映射到您的子域应用程序。使用此构造,您不会与webjar,statis内容等发生冲突。
Angular1位置html5mode等效于angular2
我见过很多人说使用connect-modrewrite之类的东西
browserSync.instance = browserSync.init({ server: { baseDir: ['app'],middleware: [ proxyMiddleware,modRewrite([ '^[^\\.]*$/index.html [L]' ]) ] },});
我设置它,但它仍然无法工作,我得到404除了主要网址之外的任何东西.
这有什么线索吗?
解决方法
PathLocationStrategy
,这是默认的LocationStrategy
注意
使用此功能时,您需要将服务器设置为将所有请求重定向到根URL.例如,如果您的用户请求myApp.com/someRoute,则将从该网址解析资产,这是不正确的.相反,您希望服务器提供index.html页面而不管指定的路由,但仍保持路由完整,以便客户端应用程序可以使用它.
另外一定要设置你的< base path =“/”/>标记在HTML的头部.
Angularjs $location html5mode浅析
关于HTML5 history API
参考资料:http://diveintohtml5.info/history.html
参考资料为英文,内容较为简单,周末会做简单翻译,方便英语吃力的童鞋。
H5之前,一个URL对应一个页面,无论以何种方式修改地址栏地址,都会导致页面完全刷新,无论跳转页面之间是否有关联。简单来说,H5的history API提供接口,可以使用javascript修改地址栏路径,而不刷新页面。
使用场景
简化说明:未设置静态缓存。
假设后端数据输出模式为:获取数据---读取模板---渲染模板。不同页面之间共享模板,只是数据内容不同。在没有H5的新接口之前,不同页面之间的跳转,则相同的脚本,CSS文件会重新加载,不仅加剧服务器压力,在慢速网络下,用户体验也会变差。在有H5接口之后,不同路径跳转,可以拦截超链接默认行为,通过脚本控制地址栏,通过Ajax加载数据,在前端完成数据呈现,就能避免不必要的资源加载时间。
Demo浅析
- 数据源
/static/articles/index.json
;/static/articles/node.json
;/static/articles/javascript.json
json
// index.json { "title": "Frontend develop language", "content": "FE encounter lots of trouble" } // node.json { "title": "Node theme develop", "content": "whether believe or not, node changes frontend develop way" } // javascript.json { "title": "Javascript theme develop", "content": "maybe javascript just tools, it enhance programmer skill" }
- 模板
/views/article.jade
(临时看的jade,所以简单为上)
jade
doctype html html head title Shuffle History script(src="/libs/superagent.js") script(src="/js/initialize.js") body h3#title | #{title} p#content | #{content} p a(href="/") Index entrance p a(href="/node/" id="node") Node entrance p a(href="/javascript/" id="javascript") Javascript entrance
总计三个页面,共享同一个jade模板。数据源对应关系为/
---------- index.json
/node/
--- node.json
/javascript
--- javascript.json
若直接访问/
,/node/
, javascript
页面,express
读取对应数据源,渲染模板,然后直接输出html
,代码如下:
javascript
/** * Created by jasonborn on 14/11/19. */ var jade = require(''jade''); var path = require(''path''); var express = require(''express''); var app = express(); app.set(''env'', ''development''); app.engine(''jade'', jade.__express); app.get(''/'', function(req, res) { res.render(''article.jade'', require(''./static/articles/index.json''), function(err, html) { res.type(''html''); res.send(html); }) }); app.get(''/node/'', function(req, res) { res.render(''article.jade'', require(''./static/articles/node.json''), function(err, html) { res.type(''html''); res.send(html); }) }); app.get(''/javascript/'', function(req, res) { res.render(''article.jade'', require(''./static/articles/javascript.json''), function(err, html) { res.type(''html''); res.send(html); }) }); app.use(express.static(path.join(__dirname, ''static''))); app.listen(1336);
自此实现所谓的MVC模式,然后使用H5 history API,所有的页面都会加载initilize.js
脚本文件:
javascript
/** * Created by jasonborn on 14/11/20. */ window.onload = function() { var title = document.querySelector(''#title''); var content = document.querySelector(''#content''); var node = document.querySelector(''#node''); var javascript = document.querySelector(''#javascript''); var resolveContent = function(target) { switch (true) { case target === ''/'': target = ''/articles/index.json''; break; case /node/.test(target): target = ''/articles/node.json''; break; case /javascript/.test(target): target = ''/articles/javascript.json''; break; } superagent .get(target) .end(function(err, res) { title.textContent = res.body.title; content.textContent = res.body.content; }) }; window.history.replaceState({ "target": window.location.pathname }, null, ''./''); window.onpopstate = function(event) { resolveContent(event.state.target); }; node.addEventListener(''click'', function(event) { event.preventDefault(); window.history.pushState({ "target": "/node/" }, null, ''/node/''); resolveContent(''/node/''); window.onpopstate = function(event) { resolveContent(event.state.target); }; }); javascript.addEventListener(''click'', function(event) { event.preventDefault(); window.history.pushState({ "target": "/javascript/" }, null, ''/javascript/''); resolveContent(''/javascript/''); window.onpopstate = function(event) { resolveContent(event.state.target); }; }); };
pushState
修改地址记录,onpopstate
表示通过后退方式退回pushState
后的路径时,执行何种操作。这里是通过Ajax
请求拉取数据,然后呈现数据(较大型项目可能会使用前后端模板引擎来渲染,例如x-template
)。
Angularjs $location html5mode
开启html5mode
,通过config
来配置即可。
javascript
angular.module(''shuffleApp'', [''ngRoute'', ''ngSanitize'']) .config([''$routeProvider'', ''$locationProvider'', function($routeProvider, $locationProvider) { $routeProvider .when(''/love'', { template:''<a href="/hate">To hate</a><p ng-bind="love"></p>'', controller: ''LoveCtrl'' }) .when(''/hate'', { template: ''<a href="/love">To love</a><p ng-bind="hate"></p>'', controller: ''HateCtrl'' }) .otherwise(''/love''); $locationProvider.html5Mode(true); }]);
上述配置,在加载入口文件之后,地址栏会变为http://hostname/love
,而不是http://hostname/#/love
。但是存在一个问题,后者可以直接访问,前者也许能访问,也许不能,但不会出现预期的结果,典型结果就是NG中文API站,每次向在新页面打开某个链接,结果就是华丽丽的报错,例如:http://docs.angularjs.cn/api,所以需要做重定向:
javascript
app.get(''/love'', function(req, res) { res.sendFile(path.join(__dirname, ''static/index.html'')); });
具体的重定向上会导致路径设计上的一些问题,所以需要注意。
联系方式
QQ: 491229492
github: https://github.com/bornkiller
AngularJS HTML5Mode
http://myapp.com/nicklewis
而不是:
http://myapp.com#/nicklewis
后者有效,但它不适合虚荣URL.
由于没有编写我自己的NodeJS应用程序来解决这个问题,Firebase中是否可以使用或不使用?
"rewrites": [ { "source": "**","destination": "/index.html" } ]
这是他们的文档中使用的代码示例,并将任何未找到的目录或文件发送回index.html.
请务必注意,您需要将firebase部署工具更新为version 1.1.0 or higher才能使其正常工作:
$npm update -g firebase-tools
根据您的权限,您可能需要使用“sudo”.
您可以在此处阅读文档:https://www.firebase.com/docs/hosting/guide/url-redirects-rewrites.html
您可以在此处阅读有关更新firebase工具的信息:https://www.firebase.com/docs/hosting/guide/command-line-tool.html
AngularJS html5mode 与第三方库冲突
如何解决AngularJS html5mode 与第三方库冲突?
我的 Angular 1.8.x 路由出现问题。
在我的 angularapp.js 文件中,我启用了 html5mode,例如:
$locationProvider.html5Mode(true);
我的 NodeJS 应用执行以下操作:
module.exports = function(express,app){
var router = express.Router();
router.get(''/*'',function(req,res){
res.render(''index.html'');
});
app.use(''/'',router);
};
但是,我确实遇到了第三方库 - Snipcart 的问题。应该做的是将电子商务功能包含到前端应用程序中。但是,Snipcart 的“结帐”按钮链接到一个带有 # 的 URL,并且 Snipcart 库不起作用(不去结帐,似乎对我所在的当前页面执行了几次循环)。
我的问题很简单 - 我该如何解决这个问题?不幸的是,html5mode 是必须的,但我还需要能够支持带有 # 的链接。
提前致谢!
解决方法
我不知道这是否可以归类为答案,但我与 Snipcart 交谈过,但无法让它发挥作用。我想 Angular 1 确实是那么旧
,您也可以考虑在您的项目中使用没有主题标签的网址
http://joeljoseph.net/angularjs-remove-hash-from-url/
我们今天的关于使用AngularJS html5Mode的Spring Boot和angular+springboot的分享就到这里,谢谢您的阅读,如果想了解更多关于Angular1位置html5mode等效于angular2、Angularjs $location html5mode浅析、AngularJS HTML5Mode、AngularJS html5mode 与第三方库冲突的相关信息,可以在本站进行搜索。
本文标签: