简体   繁体   English

Ionic/Angular 路由器无法正确重定向

[英]Ionic/Angular router doesn't redirect properly

I'm trying to build a simple ionic app, and meanwhile coding I have faced a problem in the form that the router works, when I try to access a page directly from the url(Putting the url in the browser), it redirects me to a blank url(http://localhost:8100/ to be more specific), and it should work reversely.我正在尝试构建一个简单的离子应用程序,同时编码我遇到了路由器工作形式的问题,当我尝试直接从 url 访问页面时(将 url 放在浏览器中),它重定向了我到一个空白 url(更具体地说是 http://localhost:8100/),它应该反向工作。

Hope you can help me, I tried hardly but I can't see the error.希望你能帮助我,我几乎没有尝试过,但我看不到错误。 Here is the code:这是代码:

food-list.routing.ts food-list.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { FoodListPage } from './food-list.page';

const routes: Routes = [
  {
    path: '',
    children : [
      {
        path : '',
        component : FoodListPage
      },
      {
        path: ':id',
        loadChildren : () => import('./food-detail/food-detail.module').then( m => m.FoodDetailPageModule),
      }
    ]
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class FoodListPageRoutingModule {}

app-routing.module.ts应用程序路由.module.ts

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path: 'food-list',
    loadChildren: () => import('./food-list/food-list.module').then( m => m.FoodListPageModule),
  },
  { path: '', redirectTo: '/food-list', pathMatch: 'full' },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

I already tried removing the slash from the redirect route, and nothing, if you need more of the code, please, let me know, thanks for your help我已经尝试从重定向路由中删除斜杠,没有,如果您需要更多代码,请告诉我,谢谢您的帮助

From my experience I've also never managed to get route parameters to work when they have no other static path, eg {path:':my_param'}根据我的经验,当路由参数没有其他 static 路径时,例如{path:':my_param'}

If possible I would recommend adding an intermediate path name, eg如果可能的话,我建议添加一个中间路径名,例如

{
        path: 'detail/:id',
        loadChildren : () => import('./food-detail/food-detail.module').then( m => m.FoodDetailPageModule),
      }

and updating your other routing to include the detail/ prefix.并更新您的其他路由以包含detail/前缀。

You may also have additional issues when deploying to a live server, despite the multiple paths you have there is still only 1 main file that is the entry-point for the server (your index.html file, all routes are essentially artificial), so depending on your hosting choice you should also make sure you follow their guidance to configure for server-side apps (common hosts like firebase and netlify make this very easy to do).在部署到实时服务器时,您可能还会遇到其他问题,尽管您拥有多个路径,但仍然只有 1 个主文件是服务器的入口点(您的 index.html 文件,所有路由本质上都是人为的),所以根据您的托管选择,您还应该确保遵循他们的指导来配置服务器端应用程序(firebase 和 netlify 等常见主机使这很容易做到)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM