加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何以角4路线

发布时间:2020-12-17 07:22:40 所属栏目:安全 来源:网络整理
导读:我有一个有角度的4项目,当我从localhost:4200 / route-a运行它时,它工作正常,当我刷新浏览器时,一切都按预期工作.但是,当我使用ng build构建它并从apache运行它时,导航到localhost / route-a会返回404.这是我的路由代码: imports: [BrowserModule,HttpModu
我有一个有角度的4项目,当我从localhost:4200 / route-a运行它时,它工作正常,当我刷新浏览器时,一切都按预期工作.但是,当我使用ng build构建它并从apache运行它时,导航到localhost / route-a会返回404.这是我的路由代码:
imports: [BrowserModule,HttpModule,FormsModule,RouterModule.forRoot([
    { path: 'home',component: HomeComponent },{ path: 'route-a',component: RouteAComponent },{ path: '',redirectTo: '/home',pathMatch: 'full' }
  ])]
这不是一个角度问题.这是您的Apache设置的问题.
当您在Angular中使用HTML5模式(漂亮的URL)时,您必须配置您的Apache以将服务器上不存在资源的所有请求发送到index.html文件.

这可以通过以下.htaccess配置来完成:

RewriteEngine On  
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist,use index.html
RewriteRule ^ /index.html

原因是当您尝试访问/ route-a时,apache服务器将默认尝试查找目录route-a和index.html文件.但是因为你没有该目录,apache会返回404错误.

但是通过告诉apache,在任何位置或文件不存在的请求上.要发送你的Angular html文件indsted,Angular路由器将从那里获取它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读