首页 技术 正文
技术 2022年11月11日
0 收藏 371 点赞 2,468 浏览 1033 个字

众所周知,angular项目中路由机制会在地址栏加一个#来实现各个页面的切换,虽然url中有个#号也无伤大雅,但每次看到多一个这个东西总是不舒服(我不是强迫证啊),趁着项目间隙还是决定把它去掉。

去谷哥百度一下,发现用html5的形式也解决这个问题,就一行代码,so easy.

$locationProvider.html5Mode('true');

我把这行代码加入了app.js的config中然后运行一下,发现并没有什么卵用,还报了错。

好吧,原来还要加index.html的header中加入base标签。

<base href="/" rel="external nofollow" >

OK, base标签加上之后果然运行成功,#成功去掉。

等等!

然而并没有那么简单,当我刷新页面时居然报出了404,什么鬼?!

在谷歌的帮助下我找到了原因,我还是太天真了,上边这种形式只适用于客户端的页面跳转,不适用于重新请求服务器。。。这不是坑爹吗?

终于还在谷歌的帮助下,还是让我找到了服务器端的解决方案,修改apache的配置文件,利用rewrite对发过来url地址进行重写(要确保apache中已安装rewrite模块)。

apache配置文件:

<VirtualHost *:>
ServerName my-app DocumentRoot /path/to/app <Directory /path/to/app>
RewriteEngine on # Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L] # Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>

重新启动apache,运行成功,完美去掉#。

打完收工。

参考1   https://github.com/yeoman/generator-angular/issues/433

参考2   https://scotch.io/quick-tips/pretty-urls-in-angularjs-removing-the-hashtag

参考3   http://www.tech.theplayhub.com/angularjs_routeprovider_404/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,118
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927