【问题标题】:Routing in Angular2 - GET 404 (Not Found)Angular2 中的路由 - GET 404(未找到)
【发布时间】:2017-08-06 23:11:51
【问题描述】:

所以我制作了一个运行良好的应用程序,但后来我发现顶部有两个链接的菜单栏没问题,所以我按照本教程进行操作:https://angular.io/docs/ts/latest/guide/router.html 但出了点问题。我无法让它工作,并且有这个错误: 获取http://localhost:3000/about/about.component 404(未找到) GET http://localhost:3000/home/home.component 404(未找到)

VSCode 没有显示任何问题。

主文件结构:

src
├───about
│ ├───about.component.html
│ ├───about.component.ts (and other .js and .js.map files)
├───app
│ ├───app.component.ts
│ ├───app.module.ts (and other .js and .js.map files...)
├───home
│ ├───home.component.html
│ ├───home.component.ts (other less relevant files)

在 index.html 中我添加了

app.module.ts:

import { RouterModule, Routes } from '@angular/router'
...
import { AppComponent }  from './app.component';
import { AboutComponent } from './../about/about.component';
import { HomeComponent } from './../home/home.component';

const appRoutes: Routes = [
  {
    path: '/home/home', //I've also tried home/home, home and so on
    component: HomeComponent,
  },
  {
    path: '/about/about',
    component: AboutComponent,
  },
]

@NgModule({
    imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpModule,
    ReactiveFormsModule,
    RouterModule.forRoot(appRoutes),
    ....],
      declarations: [ AppComponent, AboutComponent, HomeComponent ],
    ....

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <nav>
      <a routerLink="/home" routerLinkActive="active">Home</a>
      <a routerLink="/about" routerLinkActive="active">About</a>
    </nav>
    <router-outlet></router-outlet>
  `
})

export class AppComponent {

}

关于.component.ts:

import { Component } from '@angular/core'

@Component({
  selector: 'about',
  moduleId: module.id,
  template: `
  <p>ABOUT</p>
  `,
})

export class AboutComponent {

}

home.component.ts:

// imports

@Component({
  selector: 'home',
  moduleId: module.id,
  templateUrl: 'home.component.html',
  styleUrls: ['home.css'],
  providers: [HomeService],
})
export class HomeComponent {
// ...
}

问题可能与 routerLink 相关,但我尝试了几个不同的选项,但都不起作用。

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    访问这些路线:

    http://localhost:3000/about/
    
    http://localhost:3000/home/
    

    使用此配置:

     const appRoutes: Routes = [
          {
            path: 'home',
            component: HomeComponent,
          },
          {
            path: 'about',
            component: AboutComponent,
          },
        ]
    

    在您的HomeComponent 中使用templateUrlstyleUrls 中的相对路径,并将home.component.htmlhome.cssHomeComponent 放在同一个文件夹中:

    @Component({
      selector: 'home',
      moduleId: module.id,
      templateUrl: './home.component.html',
      styleUrls: ['./home.css'],
      providers: [HomeService],
    })
    export class HomeComponent {
    // ...
    }
    

    【讨论】:

    • 我已经试过了。不行,在控制台提示一模一样的错误。
    • 尝试在路径中不使用前导斜杠。查看更新。如果有错误则显示错误
    • 对于路径:'home' 和路径:'about' - http://localhost:3000/about/about.component Failed to load resource: the server responded with a status of 404 (Not Found)http://localhost:3000/home/home.componentFailed to load resource: the server responded with a status of 404 (Not Found)。对于 path: '/home' 这个错误是完全一样的。此外还有一条消息:message: "(SystemJS) XHR error (404 Not Found) loading http://localhost:3000/home/home.component…"
    • 在我的回答中使用这些网址:http://localhost:3000/about/http://localhost:3000/home/
    • 不幸的是,我在尝试访问 :3000/about 和 /home 时遇到了同样的错误。
    猜你喜欢
    • 2016-07-02
    • 2017-08-05
    • 2018-10-24
    • 2021-06-18
    • 2020-10-26
    • 2014-06-25
    • 2021-07-28
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多