角2:当我通过刷新浏览器出现404错误 [英] Angular 2 : 404 error occur when i refresh through Browser

查看:912
本文介绍了角2:当我通过刷新浏览器出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角2.我已经存储在我的单页的应用程序在我的服务器命名为MyApp的文件夹中。我已经在基地改为网址为的 http://example.com/myapp/
 我的项目有两个pages.So我实现了角2路由。我组默认页面login.When一种的http:// example.com/myapp/ 在我的浏览器会自动重定向到 http://example.com / MyApp的/登录。但如果刷新该页面,我得到404错误说的http://例子。 COM /的myapp /登录没有找到。

I'm new to Angular 2.I have stored my single page application in my server within a folder named as "myapp".I have changed url in base to "http://example.com/myapp/". My project have two pages.So i implement angular 2 routing .I set default page as login.When a type "http://example.com/myapp/" in my browser it will redirect automatically to "http://example.com/myapp/login" .But if refresh that page i got 404 error says that http://example.com/myapp/login not found.

但是,如果使用精简版服务器一切运行我的项目是working.In index.html中这种情况下,基础URL将是/。任何人都知道如何解决它?

But if a run my project using lite server everything is working.In this case base url in index.html will be "/". ANybody knows how to fix it ?

推荐答案

在事实上,这是正常的,你刷新你的应用程序时,因为浏览器内的实际地址更新(无需#/ hashbang方法)有一个404错误。默认情况下,HTML5历史用于Angular2重用。

In fact, it's normal that you have a 404 error when refreshing your application since the actual address within the browser is updating (and without # / hashbang approach). By default, HTML5 history is used for reusing in Angular2.

如果你想没有一个404错误,你需要更新你的服务器以服务于 index.html的文件为你定义每个路由路径。

If you want not having a 404 error, you need to update your server to serve the index.html file for each route path you defined.

如果你想切换到HashBang方法,您需要使用此配置:

If you want to switch to the HashBang approach, you need to use this configuration:

import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {LocationStrategy, Location, HashLocationStrategy } from 'angular2/router'; 

import {MyApp} from './myapp';

bootstrap(MyApp, [
  ROUTER_PROVIDERS,
  provide(LocationStrategy, {useClass: HashLocationStrategy}
]);

在此情况下,当您刷新页面,它会再次显示(但你将有一个您的地址)。

In this case, when you refresh the page, it will be displayed again (but you will have a # in your address).

这个链接可以帮助你还有:<一href=\"http://stackoverflow.com/questions/34415725/when-i-refresh-my-website-i-get-a-404-this-is-with-angular2-and-firebase/\">When我刷新我的网站,我收到了404这是Angular2和火力。

This link could help you as well: When I refresh my website I get a 404. This is with Angular2 and firebase.

希望它可以帮助你,
蒂埃里

Hope it helps you, Thierry

这篇关于角2:当我通过刷新浏览器出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆