我想为我的应用启用HTML5模式.我已经把下面的代码的配置,如图所示在这里:
return app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix = '!';
$routeProvider.when('/', {
templateUrl: '/views/index.html',
controller: 'indexCtrl'
});
$routeProvider.when('/about',{
templateUrl: '/views/about.html',
controller: 'AboutCtrl'
});
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我使用了$locationProvider.html5mode
和我改变了我所有的链接ng-href
来排除/#/
.
目前,我可以去localhost:9000/
看看索引页面并导航到其他页面localhost:9000/about
.
但是,刷新localhost:9000/about
页面时会出现问题.我得到以下输出:Cannot GET /about
如果我看一下网络电话:
Request URL:localhost:9000/about
Request Method:GET
Run Code Online (Sandbox Code Playgroud)
如果我先去localhost:9000/
,然后点击导航到的按钮,/about
我会得到:
Request URL:http://localhost:9000/views/about.html
Run Code Online (Sandbox Code Playgroud)
这使页面完美呈现.
如何在刷新时启用角度来获取正确的页面?
我想在Apache服务器上部署Angular 2应用程序.我读过各种指南喜欢这个和这个,但他们没有工作.我已经npm
和ng
安装在服务器上.
简而言之,这就是我所做的:
npm install
.ng build --prod
命令,它创建了一个dist
目录.apache
root 更改为/var/www/html/dist
目录.mod_rewrite
,重新启动apache
并.htaccess
在我的dist
目录中添加它.<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
但是,只有我的主页domain.com
的作品,其他页面一样domain.com/login
,domain.com/register
等等抛出404错误.即使domain.com/index.html/login
不起作用.
该应用程序在我正在使用它开发它的本地系统上正常工作ng serve
.我错过了什么?
我的Angular 2应用程序使用默认的HTML 5 history.pushState
位置策略.如何使用history.pushState
浏览器刷新配置服务器并且浏览器URL不生成404
s?
例如,Tour of Heroes应用程序有一些不同的应用程序路径,例如:
http://localhost:8080/dashboard
http://localhost:8080/heroes
http://localhost:8080/detail/13
Run Code Online (Sandbox Code Playgroud)
如果你打刷新在这些航线上的一个,或键入一个URL,你会得到404
,因为/detail/13
是一个应用程序的路线,而不是服务器资源.我已将位置策略配置head
在我的顶部index.html
:
<!doctype html>
<html>
<head>
<base href="/">
<meta charset="utf-8">
<title>Tour of Heroes</title>
Run Code Online (Sandbox Code Playgroud)
但是如何配置服务器以便将所有URL发送到我的应用程序而不是生成404
?
注意:这个问题是问题的补充当我通过浏览器刷新Angular 2:404错误时,Angular 2.0路由器无法重新加载浏览器,询问如何解决此问题.对于Firebase,有这样的:当我刷新我的网站时,我得到一个404.这是使用Angular2和firebase,对于Apache,这就是:Angular路由的htaccess重定向.
我读过 AngularJS 使用 hashbang URL 作为默认值 - 但这不是一个优势,因此应该使用 HTML5 URL。为了在客户端配置此行为,必须完成以下操作:
...
$locationProvider.html5Mode(true);
...
Run Code Online (Sandbox Code Playgroud)
在服务器端还必须完成一些配置 - 据我了解应该配置 URL 请求,因此应该将包含 ng-app 的页面(起始页面 - 例如index.html)传递给客户端。不包括提供静态资源(CSS、图像、AngularJS 部分等)的 URL 和用于 CRUD 的 URL(例如 RESTful 服务 URL)。
我的问题不是如何在服务器端调整这种行为(例如,对于内部有嵌入式服务器的字符串启动)?
另一个问题是,您是否在 AngularJS 应用程序中区分静态资源(例如 .../static/..)和 CRUD URL(例如 .../database/...)?
多谢!
我已经使用AngularJS在项目上设置了路由,以在单击菜单按钮时加载其他模板。该模板基于浏览器URL。唯一的问题是,直接访问URL时不会加载模板。相反,它将加载空白页(找不到对象)。如何克服呢?
因此,该项目位于文件夹/ project4中的本地驱动器上。共有三个菜单按钮,其中两个链接到:
project4/about
和 project4/contact
这是HTML部分:
<html>
<head>
<base href="/ordina/angular/project4/">
<link href=
"//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel=
"stylesheet">
<link href=
"//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel=
"stylesheet">
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="script.js"></script>
<title></title>
</head>
<body>
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing
Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li>
<a href=""><i class="fa fa-home"></i> Home</a>
</li>
<li>
<a href="about"><i class="fa fa-shield"></i> About</a>
</li>
<li>
<a href="contact"><i class="fa fa-comment"></i>
Contact</a>
</li>
</ul>
</div>
</nav>
</header>
<div id="main">
<div>
<p>This text …
Run Code Online (Sandbox Code Playgroud) angularjs ×3
angular ×2
.htaccess ×1
angular2-cli ×1
apache ×1
html5 ×1
javascript ×1
spring ×1
spring-boot ×1
url-routing ×1