欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决Angular4项目部署到服务器上刷新404的问题

程序员文章站 2023-11-25 17:21:04
刚遇到angular4项目npm run build 后部署到服务器可以访问,但是刷新页面会出现404的错误!在网上搜了一下看到有些人写的解决办法还有错误在里面,一眼看就知...

刚遇到angular4项目npm run build 后部署到服务器可以访问,但是刷新页面会出现404的错误!在网上搜了一下看到有些人写的解决办法还有错误在里面,一眼看就知道的错误,感觉这些人真的有毛病,决定写了博客,为什么那么不仔细,写错误的上去,*别人吗!!

解决angular2页面刷新后报404错误办法:

配置app.module.ts

import {hashlocationstrategy , locationstrategy} from '@angular/common';

@ngmodule({
 declarations: [appcmp],
 bootstrap: [appcmp],
 imports: [browsermodule, routes],
 providers: [{provide: locationstrategy, useclass: hashlocationstrategy}]
});

主要添加的代码:

providers: [{provide: locationstrategy, useclass: hashlocationstrategy}]

这样设置后,访问angular站点,会自动在根节点后面加一个#锚点。再次刷新便不会报404错误了。

以上这篇解决angular4项目部署到服务器上刷新404的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。