2017-08-08 182 views
1

我構建了一個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); 
 
    } 
 

 
}

我又寫道產品servicce這樣的:

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); 
 
    }

我不能recvie產品信息。

+0

控制檯中的任何錯誤? – Brad

+0

您可以點擊鏈接頂部的'在這裏輸入圖片描述' –

+0

當您的組件第一次加載'product'時未定義,並且您的模板會引發錯誤。在@Sajeetharan的回答中建議的模板中使用'?'安全運算符。 – Brad

回答

1

使用safe operator (?)檢查值存在,

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

謝謝,但我無法從我的Arry中找到信息 –