【发布时间】: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>
ProductListComponent 在 app.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