javascript - Angular 依赖注入(inject)

标签 javascript angular typescript

我建立了一个 Angular 2 网站。但我不知道哪里出了问题。我从一周开始就尝试调试这个问题,但仍然找不到任何解决方案。我会展示我的代码。如果您需要更多详细信息。我会解释更多。 我无法传递信息以从我的服务中查看。 错误:enter image description here

我这样写我的 productdetail.component.html:

<div class="thumbnail">
  <img src="http://via.placeholder.com/820x230">
  <div>
   <h4>hi</h4>
    <h4>{{product.title}}</h4>
  </div>
</div>

我这样写我的 productdetail.component.ts :

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Product, ProductService, Comment } from '../shared/product.service';

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

  product: Product;
  comments: Comment [ ];
  constructor(private  routeInfo: ActivatedRoute, private productService: ProductService) { }
  ngOnInit() {
   const productId: number = this.routeInfo.snapshot.params['productId'];
   console.log(productId);
   this.product = this.productService.getProduct(productId);
   this.comments = this.productService.getCommentsForProduct(productId);
  }

}

我这样写产品服务:

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

@Injectable()
export class ProductService {
  public products: Product[] = [
    new Product(1, 'Apple8', 8000, 5, 'new brand phone', ['phone', 'electronic products']),
    new Product(2, 'Table', 800, 4, 'white dinner table', ['furniture']),
    new Product(3, 'PC', 1000, 3, 'window 10 and I7', ['Desktop', 'electronic products']),
    new Product(4, 'LabTop', 600, 3.5, 'brand new SSID drive hard', ['labtap', 'electronic products']),
    new Product(5, 'oil heater', 50, 2.5, 'one years old  and works', ['heater', 'electronic products'])];
  public commends: Comment[] = [
     new Comment(1 , 1 , '2017-8-7:19:00' , 'Lili' , 3 , 'not so good'),
    new Comment(2 , 1 , '2017-8-8:19:30' , 'Lulu' , 4 , ' Thanks'),
    new Comment(3 , 2 , '2017-8-9:19:00' , 'Anna' , 3 , 'normal'),
    new Comment(4 , 3 , '2017-8-3:19:00' , 'yard' , 1, 'I do not like it'),
    new Comment(5 , 4, '2017-8-4:19:00' , 'Jane' , 2 , 'very good'),
    new Comment(6 , 5 , '2017-8-6:19:00' , 'Rob' , 5 , 'Excellent')];
  constructor() { }
  getProducts( ): Product[] {
    return this.products;
  }
  getProduct( id: number): Product {
    return this.products.find(item => item.id === id);
  }
  getCommentsForProduct( id: number): Comment[] {
    return this.commends.filter((comment: Comment ) => comment.productId === id );
  }

}
export class  Product {
  constructor(
    public id: number,
    public title: string,
    public price: number,
    public rating: number,
    public desc: string,
    public  categories: Array<string>
  ) {
  }
}

export class  Comment {

  constructor(public id: number, public productId: number, public timestamp: string,
              public user: string, public rating: number, public content: string) {
  }
}

我这样写 app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { NavberComponent } from './navber/navber.component';
import { FooterComponent } from './footer/footer.component';
import { SearchComponent } from './search/search.component';
import { CarouseComponent } from './carouse/carouse.component';
import { ProductComponent } from './product/product.component';
import { StarsComponent } from './stars/stars.component';
import { ProductDetailComponent } from './product-detail/product-detail.component';
import { HomeComponent } from './home/home.component';
import {RouterModule, Routes} from '@angular/router';
import {ProductService} from './shared/product.service';

const  routeConfig: Routes = [
  {path: 'product/:productId', component: ProductDetailComponent},
  {path: '', component: HomeComponent}

];




@NgModule({
  declarations: [
    AppComponent,
    NavberComponent,
    FooterComponent,
    SearchComponent,
    CarouseComponent,
    ProductComponent,
    StarsComponent,
    ProductDetailComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(routeConfig)
  ],
  providers: [ProductService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我检查了路线,它运作良好,我猜这个方法不起作用。 但我认为我的语法是正确的 这个方法:

getProduct( id: number): Product {
    return this.products.find(item => item.id === id);
  }

我无法收到产品信息。

最佳答案

使用 safe operator (?) 检查值是否存在,

<div class="thumbnail">
  <img src="http://via.placeholder.com/820x230">
  <div>
   <h4>hi</h4>
    <h4>{{product?.title}}</h4>
  </div>
</div>

关于javascript - Angular 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558096/

相关文章:

javascript - 将 div 从其位置动画到全屏

angular - navigator.geolocation.watchPosition 返回位置不可用

css - 如何从同一模式内的选择下拉列表中选择选项时设置模式的边距

typescript - 如何知道 ngOnChanges 中的哪些 @Input 变化?

javascript - 找不到别名。也许你忘了加入它

javascript - 将变量从一个 HTML 页面传递到另一个 PHP 页面

javascript - 通过脚本弹出widget

javascript - 如何在 2 个变量(其中一个是数组)上使用 $watchgroup 或 $watchCollection?

具有多个模块的 Angular 2 项目结构

javascript - 如何让 Promise 在已经解决时不重新运行代码?