Angularjs 带有grunt服务的html5模式不起作用
在grunt文件中,livereload块如下所示:Angularjs 带有grunt服务的html5模式不起作用,angularjs,gruntjs,Angularjs,Gruntjs,在grunt文件中,livereload块如下所示: livereload:{ 选项:{ 开放:是的, 中间件:功能(连接、选项、中间件){ var optBase=(typeof options.base=='string')?[options.base]:options.base; 返回[ [require('connect-modrewrite')(['!(\\..+)$/[L]'))].concat( 映射(函数(路径){ 返回connect.static(路径); })), conne
livereload:{
选项:{
开放:是的,
中间件:功能(连接、选项、中间件){
var optBase=(typeof options.base=='string')?[options.base]:options.base;
返回[
[require('connect-modrewrite')(['!(\\..+)$/[L]'))].concat(
映射(函数(路径){
返回connect.static(路径);
})),
connect.static('.tmp'),
连接()。使用(
“/bower_组件”,
连接.静态(“./bower_组件”)
),
连接()。使用(
“/app/styles”,
connect.static(“./app/styles”)
),
connect.static(appConfig.app)
];
}
}
},
添加:
[require('connect-modrewrite')(['!(\\..+)$ / [L]'])].concat(
optBase.map(function(path){ return connect.static(path); })),
曾经为我启用html5模式,否则,我的路由在没有#的情况下不会加载!当我尝试通过浏览器重新加载时
我确实在配置中添加了base href='/'和html5Mode(true)。还有什么我可以试试的吗?为什么它真的会停止工作
注意:原来我的URL中有一个点,连接修改重写规则无法很好地处理这个点。你知道如何改变它并使其能够处理URL中的点吗?
尝试在配置块的根应用程序模块中添加此。确保包括/注入$locationProvider
$locationProvider
.HTML5模式({
启用:对,
requireBase:错误
})
.hashPrefix(“!”);
我认为网点问题是一般问题的副作用。或多或少都有相同的设置。我只需在.config()
中重置HTML5模式
,如下所示:
if (window.location.host == 'localhost:9000') {
$locationProvider.html5Mode(false);
$locationProvider.hashPrefix('');
} else {
$locationProvider.html5Mode(true);
}
在开发模式下,该应用程序仍然会在所有URL前面加上#/
,这使得URL、链接和livereload都很麻烦。通过将常规的/url
更改为/#/url
,解决了这个问题:
var isLocalHost = (location.hostname === "localhost" || location.hostname === "127.0.0.1");
angular.module('myApp').directive('debugLink', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$timeout(function() {
var href = element.attr('href');
if (href && isLocalHost && href.charAt(0) == '/') {
element.attr('href', '#'+href);
}
}, 100);
}
}
});
现在可以通过
最后,我使用生产服务器上的.htaccess
重定向
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]
我在几个angularjs+grunt项目中使用了这种方法。它可以防止livereload被破坏,也就是说,当我点击Ctrl-S时,页面会被刷新,我可以在任何地方使用正确的URL。我想没有人对在生产模式中使用前缀或哈希感兴趣 您是否已将应用程序配置配置配置为使用html5模式?是的………..结果表明,我的URL中有一个点,而连接修改重写规则无法很好地处理该点。你知道如何改变这一点并使其能够处理URL中的点吗?@SimranKaur你用的是什么?你也配置了吗?@SimranKaur backend。