Javascript 如何实现`$locationProvider.html5Mode(true);
据我所知,Javascript 如何实现`$locationProvider.html5Mode(true);,javascript,html,angularjs,Javascript,Html,Angularjs,据我所知,$locationProvider.html5Mode(true)从URL中删除哈希? 虽然对我来说只有主页在刷新页面时看起来是正常的。 这不正是重定向到:应该处理的吗 var rustyApp = angular.module('rustyApp', ['ngRoute','viewController', 'mm.foundation','angular-flexslider','ui.router'], function($routeProvider, $locationProv
$locationProvider.html5Mode(true)代码>从URL中删除哈希?
虽然对我来说只有主页在刷新页面时看起来是正常的。
这不正是重定向到:
应该处理的吗
var rustyApp = angular.module('rustyApp', ['ngRoute','viewController',
'mm.foundation','angular-flexslider','ui.router'],
function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/partials/home.html',
controller: 'HomeController'
});
// When you put /home, it also automatically handles /home/ as well
$routeProvider.when('/work', {
templateUrl: '/partials/work.html',
controller: 'WorkController'
});
$routeProvider.when('/contact', {
templateUrl: '/partials/contact.html',
controller: 'ContactController'
});
$routeProvider.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true).hashPrefix('!');
});
这是我的html:
这是在头部:
<base href="/"/>
到
并将其添加到.htaccess
文件中
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</IfModule>
重新启动发动机
RewriteCond%{REQUEST_FILENAME}-s[或]
RewriteCond%{REQUEST_FILENAME}-l[或]
RewriteCond%{REQUEST_FILENAME}-d
重写规则^.*$-[NC,L]
重写规则^(.*)/index.html[NC,L]
然而,当我通过apache运行服务器时,这似乎是可行的。
我想我的问题是gulp生成的静态服务器没有配置为处理该请求。我确信有一些附加的
gulp open
等功能。从on$location
上,它说您需要对HTML5模式进行服务器端重写才能进行页面刷新。详细说明不同的服务器配置 在$location
上,它指出您需要为HTML5模式进行服务器端重写才能使用页面刷新。详细说明不同的服务器配置 这是因为Web服务器将首先处理请求,因此,如果您没有设置任何形式的URL重写,它只会在Web服务器中查找您在“路由”中输入名称的文件夹
如果您使用的是Node Express Web服务器,可以如下设置重写:
app.all('/*', function(req, res) {
res.sendfile(__dirname + '/index.html'); // Or whatever your public folder is
});
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/#/$1 // Might have to fiddle with the hash and/or other prefix you want to use
</IfModule>
对于Apache服务器,您可以使用.htaccess,如下所示:
app.all('/*', function(req, res) {
res.sendfile(__dirname + '/index.html'); // Or whatever your public folder is
});
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/#/$1 // Might have to fiddle with the hash and/or other prefix you want to use
</IfModule>
重新启动发动机
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
RewriteRule^(.*)/index.php/#/$1//可能需要修改哈希和/或其他要使用的前缀
这是因为Web服务器将首先处理请求,因此,如果您没有设置任何形式的URL重写,它只会在Web服务器中查找您在“路由”中输入名称的文件夹
如果您使用的是Node Express Web服务器,可以如下设置重写:
app.all('/*', function(req, res) {
res.sendfile(__dirname + '/index.html'); // Or whatever your public folder is
});
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/#/$1 // Might have to fiddle with the hash and/or other prefix you want to use
</IfModule>
对于Apache服务器,您可以使用.htaccess,如下所示:
app.all('/*', function(req, res) {
res.sendfile(__dirname + '/index.html'); // Or whatever your public folder is
});
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/#/$1 // Might have to fiddle with the hash and/or other prefix you want to use
</IfModule>
重新启动发动机
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
RewriteRule^(.*)/index.php/#/$1//可能需要修改哈希和/或其他要使用的前缀
Hmmm,从它的外观上看,服务器配置看起来像是将被放入httpd.conf
的东西。您是否曾经配置SPA来使用$location
对于我创建的应用程序,我没有使用$location
的HTML5模式。一部分是因为我所处的环境,另一部分是因为除了美观之外,它真的没有必要。嗯,从它的外观来看,服务器配置看起来像是要放入httpd.conf
中的东西。您是否为我创建的应用程序配置过SPA来使用$location
,我没有使用$location
的HTML5模式。一部分是因为我所处的环境,另一部分是因为除了美学之外,这真的没有必要。嘿,谢谢你回答我的问题!现在我正在使用gulp在本地运行一个通用服务器,但是我是否正确地假设,我应该使用上面的方法(对于Apache服务器,因为我使用expresswebserver)但是将index.php
部分更改为index.html
将所有内容保存在.htaccess
文件中,并将其保存在与我的开发文件夹相同的目录中?你不知道这有多大帮助!!!!干杯嘿,谢谢你回答我的问题!现在我正在使用gulp在本地运行一个通用服务器,但是我是否正确地假设,我应该使用上面的方法(对于Apache服务器,因为我使用expresswebserver)但是将index.php
部分更改为index.html
将所有内容保存在.htaccess
文件中,并将其保存在与我的开发文件夹相同的目录中?你不知道这有多大帮助!!!!干杯