【问题标题】:How to fix routing in my Angular component used in ASP.NET Core web app?如何修复 ASP.NET Core Web 应用程序中使用的 Angular 组件中的路由?
【发布时间】:2020-11-11 04:29:08
【问题描述】:

我关注Angular tutorial from documentation,它来到了路由。这是一个试用示例应用程序,主要区别在于我的示例与 ASP.NET Core 2.2 Web 应用程序集成,Angular 组件显示在 .cshtml 视图中。

请注意,我在我的 ASP.NET Core 视图中使用 Angular 组件选择器,就像在 index.cshtml 中这样,所有 Angular 视图都在其中呈现:

Index View

<current-time></current-time>
<app-top-bar></app-top-bar>
<app-product-list></app-product-list>

一切都显示得很好,直到我尝试在 product-list.component.html 中添加到 ProductListComponent 的路由:

    <h2>Products</h2>

<div *ngFor="let product of products">

  <h3>
    <a [title]="product.name + ' details'" [routerLink]="['/products', product.productId]">
      {{ product.name }}
    </a>
  </h3>

  <p *ngIf="product.description">
    Description: {{ product.description }}
  </p>

  <button (click)="share()">
    Share
  </button>

  <app-product-alerts [product]="product"
                      (notify)="onNotify()">
  </app-product-alerts>

</div>

ProductListComponentapp.module.ts 中声明如下:

import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { NgModule, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { APP_BASE_HREF } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';

import { CurrentTimeComponent } from './current-time/current-time.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductAlertsComponent } from './product-alerts/product-alerts.component';
import { ProductDetailsComponent } from './product-details/product-details.component';

@NgModule({
  declarations: [
    CurrentTimeComponent,
    TopBarComponent,
    ProductListComponent,
    ProductAlertsComponent,
    ProductDetailsComponent
  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    RouterModule.forRoot([
      { path: '', component: ProductListComponent },
      { path: 'products/:productId', component: ProductDetailsComponent },
    ])
  ],
  exports: [
    CurrentTimeComponent,
    TopBarComponent,
    ProductListComponent
  ],
  providers: [{ provide: APP_BASE_HREF, useValue: '/' }], //for ASP.NET Core
})
export class AppModule {
  constructor(private injector: Injector) {
  }

  ngDoBootstrap() {
    customElements.define('current-time', createCustomElement(CurrentTimeComponent, { injector: this.injector }));
    customElements.define('app-top-bar', createCustomElement(TopBarComponent, { injector: this.injector }));
    customElements.define('app-product-list', createCustomElement(ProductListComponent, { injector: this.injector }));
  }
}

根据教程,在 product-list 视图中将鼠标指针悬停在 product 上时,我应该收到锚路径:http://localhost:59119/products/1,但我收到的是:http://localhost:59119/products/undefined

另一件事是,当我将浏览器导航到http://localhost:59119/products/1 时,我会收到404

请注意,在我的 app.module.ts 中我没有 bootstrap: [ AppComponent]。我假设,如果 Angular 视图由 .cshtml 视图显示,我不需要这个。它有什么改变吗?我开始怀疑是的。

无论如何,有什么方法可以让我导航到产品详细信息吗?

product-details.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { products } from '../products';

@Component({
  selector: 'app-product-details',
  templateUrl: './product-details.component.html',
  styleUrls: ['./product-details.component.css']
})
export class ProductDetailsComponent implements OnInit {
  product;

  constructor(
    private route: ActivatedRoute,
  ) { }

  ngOnInit() {
    this.route.paramMap.subscribe(params => {
      this.product = products[+params.get('productId')];
    });
  }

}

productc.ts

export const products = [
  {
    productId: 1,
    name: 'Phone XL',
    price: 799,
    description: 'A large phone with one of the best screens'
  },
  {
    productId: 2,
    name: 'Phone Mini',
    price: 699,
    description: 'A great phone with one of the best cameras'
  },
  {
    productId: 3,
    name: 'Phone Standard',
    price: 299,
    description: ''
  }
];

product-list.component.ts

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

import { products } from '../products';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {
  products = products;

  share() {
    window.alert('The product has been shared!');
  }

  onNotify() {
    window.alert('You will be notified when the product goes on sale');
  }

  constructor() { }

  ngOnInit() {
  }
}

【问题讨论】:

  • 对于第二个问题,product.productId 似乎不存在。您可以在您的问题中发布product-list.component.ts 吗?
  • @bakunet,你不能这样做。您的 main.app.module 有一个 bootstrap 标记。这是您在 index.chtml 中需要的组件(该组件的选择器)
  • @Eliseo 我添加到 app.module.ts bootstrap: [ProductListComponent], 和相同的结果。
  • 我会尝试玩这种路由,我们会看到...stackoverflow.com/a/49414757/12603542
  • @Eliseo 好吧,我现在注意到,我的方法完全错误。实际上,与其在我的 ASP 应用程序中使用单个组件,不如使用几个 Angular 应用程序,并在它们内部进行路由。我明白了,我想做的方式不受支持,或者换一种说法,我做错了:)

标签: angular typescript routes asp.net-core-2.2


【解决方案1】:

对于您在未定义时提出的第一个问题

http://localhost:59119/products/undefined

为此,您必须更新 html -

<div *ngFor="let product of products">

  <h3>
    <a [title]="product.name + ' details'" [routerLink]="['/products', productId]">
      {{ product.name }}
    </a>
  </h3>(...)

</div>

这里 productId 未定义,这就是您收到错误的原因。

对于第二个问题,您会收到404,因为这里

  { path: 'products/:productId', component: ProductDetailsComponent },

如您所见,该路径 ProductDetailsComponent 是为该路由定义的,而 angular 不会路由到 cshtml 文件。

【讨论】:

  • 广告1)如果产品是用&lt;div *ngFor="let product of products"&gt;迭代的,TS中没有隐含指定ID?广告 2)不应该以某种方式在 SPA 内部路由吗?
  • productId 未在您的代码中的任何位置定义。如果 id 出现在该处,您能否检查产品对象并在 html 中使用该 id,如 product.id。
  • 我刚刚在 products.ts 中使用 products 集合定义更新了问题正文。实际上没有productId,但是添加后,我得到了相同的结果。
  • 如果没有 productid,您将如何使用该 id 进行路由?
  • 在html中你必须在路由器链接中使用product.productId
猜你喜欢
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 2017-10-31
  • 2019-01-26
  • 2021-02-02
  • 2020-11-04
相关资源
最近更新 更多