首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular routing在生产中不起作用(aruba托管)

Angular routing在生产中不起作用(aruba托管)
EN

Stack Overflow用户
提问于 2021-09-11 16:58:13
回答 1查看 73关注 0票数 1

我知道这可能是重复的,但我在其他问题上没有找到任何解决方案。我在生产中遇到了一些关于angular路由的问题。我尝试了很多解决方案,它只适用于{useHash: true}。我的最后一次尝试是在项目的根目录中设置.htaccess,并尝试不同的配置。

我的怀疑是阿鲁巴服务器没有配置好,当路由被触发时不能回退到index.html。当我访问thearkexperience.studio/login时,服务器会将我重定向到https://admin.aruba.it/PannelloAdmin/Login.aspx,这非常奇怪。

下面是我的代码:

index.html有<base href="/">

app-routing.component.ts

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {BodyComponent} from "./components/layout/home/body/body.component";
import {HomeComponent} from "./components/layout/home/home.component";

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./components/layout/home/home.module').then(m => m.HomeModule),
  },
  {
    path: '**',
    redirectTo: ''
  }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

home-routing.component.ts

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {HomeComponent} from "./home.component";
import {BodyComponent} from "./body/body.component";
import {LoginMobileComponent} from "../../pages/custom/login_mobile/login_mobile.component";
import {LoginComponent} from "../../pages/custom/login/login.component";
import {Custom_1Component} from "../../pages/custom/custom_1/custom_1.component";
import {Custom_2Component} from "../../pages/custom/custom_2/custom_2.component";
import {Custom_3Component} from "../../pages/custom/custom_3/custom_3.component";

const routes: Routes = [
  {
    path: '',
    children: [
      {
        path: 'login',
        component: LoginComponent
      },
      {
        path: 'm/login',
        component: LoginMobileComponent
      },
      {
        path: '',
        component: BodyComponent
      },
      {
        path: ':page',
        component: BodyComponent
      },
      {path: '**', redirectTo: ''}
    ]
  },
  {path: '**', redirectTo: ''}
];
@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomeRoutingModule { }

.htaccess

代码语言:javascript
复制
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-11 17:03:30

Angular应用程序是单页面应用程序(也就是说,它们应该始终只提供一个页面-即index.html)。基本上,您会发现,如果您单击任何路由上的刷新,您将得到问题。这是因为您的when服务器正在尝试为/route提供服务,而它应该始终为index.html提供服务。

您需要将prod the服务器配置为始终为index.html提供服务,而不管路由是什么。如何做到这一点在不同的How服务器上有所不同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69145043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档