如何以角4路线

前端之家收集整理的这篇文章主要介绍了如何以角4路线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个有角度的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路由器将从那里获取它.

原文链接:https://www.f2er.com/angularjs/141787.html

猜你在找的Angularjs相关文章