DAN*_*DAN 164 angular2-routing angular
我是Angular 2的新手.我已将我的单页应用程序存储在名为"myapp"的文件夹中的服务器中.我已将基础中的网址更改为http://example.com/myapp/ `.
我的项目有两页.所以我实现了Angular 2路由.我将默认页面设置为登录.当我http://example.com/myapp/在浏览器中输入时,它会自动重定向到http://example.com/myapp/login.但如果刷新该页面我会收到404错误,说http://example.com/myapp/login找不到.
但如果我使用lite服务器运行我的项目,一切正常.在这种情况下,index.html中的URL将是"/".如何解决?
Asa*_*nel 230
在app.module.ts中:
添加进口:
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
Run Code Online (Sandbox Code Playgroud)在NgModule提供程序中,添加:
{provide: LocationStrategy, useClass: HashLocationStrategy}
Run Code Online (Sandbox Code Playgroud)示例(app.module.ts):
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
bootstrap: [AppComponent],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
将RouterModule.forRoot与{useHash:true}参数一起使用.
示例:(来自 angular docs)
import { NgModule } from '@angular/core';
...
const routes: Routes = [//routes in here];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(routes, { useHash: true })
],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
Thi*_*ier 123
事实上,刷新应用程序时出现404错误是正常的,因为浏览器中的实际地址正在更新(并且没有#/ hashbang方法).默认情况下,HTML5历史记录用于在Angular2中重用.
要修复404错误,您需要更新服务器以为index.html您定义的每个路径路径提供文件.
如果要切换到HashBang方法,则需要使用此配置:
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
import {MyApp} from './myapp';
bootstrap(MyApp, [
ROUTER_PROVIDERS,
{provide: LocationStrategy, useClass: HashLocationStrategy}
]);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,当您刷新页面时,它将再次显示(但您#的地址中将有一个).
这个链接也可以帮到你:当我刷新我的网站时,我得到了404.这是使用Angular2和firebase.
希望它对你有帮助,蒂埃里
Fra*_* Yu 103
对于真正想要PathLocationStrategy(例如html5Mode)的人(比如我)HashLocationStrategy,请参阅如何:配置您的服务器以使用来自第三方wiki的html5Mode:
启用html5Mode后,
#您的网址中将不再使用该字符.该#符号很有用,因为它不需要服务器端配置.没有#,URL看起来更好,但它也需要服务器端重写.
在这里我只复制wiki中的三个例子,万一Wiki丢失了.通过搜索关键字"URL重写"(例如Firebase的这个答案)可以找到其他示例.
<VirtualHost *:80>
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>
Run Code Online (Sandbox Code Playgroud)
server {
server_name my-app;
root /path/to/app;
location / {
try_files $uri $uri/ /index.html;
}
}
Run Code Online (Sandbox Code Playgroud)
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
小智 24
我有同样的问题.我的Angular应用程序在Windows服务器上运行.
我通过在根目录中创建web.config文件解决了这个问题.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Niy*_*yaz 20
也许您可以在使用RouterModule注册根目录时执行此操作.您可以传递具有属性"useHash:true"的第二个对象,如下所示:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ROUTES } from './app.routes';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
RouterModule.forRoot(ROUTES ,{ useHash: true }),],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
对于使用Angular 2 rc4或更高版本的人来说,看起来LocationStrategy已经从路由器转移到了普通路由器.你必须从那里导入它.
另请注意'provide'行周围的花括号.
main.ts
// Imports for loading & configuring the in-memory web api
import { XHRBackend } from '@angular/http';
// The usual bootstrapping imports
import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS } from '@angular/http';
import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes';
import { Location, LocationStrategy, HashLocationStrategy} from '@angular/common';
bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
HTTP_PROVIDERS,
{provide: LocationStrategy, useClass: HashLocationStrategy}
]);
Run Code Online (Sandbox Code Playgroud)
如果您在Visual Studio 2015中通过ASP.NET Core 1运行Angular 2,您可能会发现JürgenGutsch的此解决方案很有帮助.他在一篇博文中描述了这一点.这对我来说是最好的解决方案.在app.UseStaticFiles()之前,将下面提供的C#代码放在Startup.cs public void Configure()中;
app.Use( async ( context, next ) => {
await next();
if( context.Response.StatusCode == 404 && !Path.HasExtension( context.Request.Path.Value ) ) {
context.Request.Path = "/index.html";
await next();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
164820 次 |
| 最近记录: |