【问题标题】:Route not working what is wrong in my code路由不起作用我的代码有什么问题
【发布时间】:2019-11-06 06:35:44
【问题描述】:

我的 app-routing.module.ts 中有这条路线缺少一些东西,因为 当我进入这个页面时

https://angular-v4-dot-unique-yew-244216.appspot.com/search

我收到错误,但是当我去这里时它可以工作

https://angular-v4-dot-unique-yew-244216.appspot.com

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

import { SearchBooksComponent } from './search-books/search-books.component';

const routes: Routes = [
  {
    path: '',
    redirectTo: '/search',
    pathMatch: 'full'
  },
  {
    path: 'search',
    component: SearchBooksComponent
  }
];

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

路线有什么问题?

这是我的 app.yaml

runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
  static_files: dist/index.html
  upload: dist/index.html
- url: /
  static_dir: dist

【问题讨论】:

标签: angular routes


【解决方案1】:

你应该重定向到search而不是/search

const routes: Routes = [
  {
    path: '',
    redirectTo: 'search',
    pathMatch: 'full'
  },
  {
    path: 'search',
    component: SearchBooksComponent
  }
];

路由顺序仅在两条路由冲突时才重要,如search**search 将被两条路线匹配,第一个获胜。

您可以使用像this 这样的哈希定位策略。 这个问题也解释了路径定位策略。

【讨论】:

【解决方案2】:

我们设置路线的顺序至关重要。尝试这个!。 最后设置默认路由。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SearchBooksComponent } from './search-books/search-books.component';

const routes: Routes = [
 { path: 'search', component: SearchBooksComponent },
 { path: '', redirectTo: 'search',pathMatch: 'full'}
];

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

【讨论】:

  • 奇怪,这适用于 LocalHost 但不适用于 GCP,尝试链接 https://angular-v4-dot-unique-yew-244216.appspot.com/search 应该重定向但不能。删除 /search 就可以了
  • @Erik 在这种情况下,路线顺序无关紧要,请查看我的答案。重要的想法是search 而不是/search
  • 部署到 GCP 后我真的得到了:错误:未找到在此服务器上未找到请求的 URL/搜索。
猜你喜欢
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 2016-12-02
  • 2022-01-19
  • 2021-10-29
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多