angular - 通过服务在两个不同的组件之间传递数据
问题描述
我试图传递'ele',它是一个包含关于我的食谱的详细信息的数组recipe-item.component.ts
,recipe-detail.component.ts
并且成功并在调用的变量中接收了它,selectedrecipe
但是当我尝试在recipe-detail.component.html
via{{selectedrecipe.imagepath}}
和中显示详细信息时。{{selectedrecipe.name}}
它抛出错误并说 ERROR
TypeError:无法读取 Object.eval [as updateRenderer] (RecipeDetailsComponent.html:3) 处未定义的属性“图像路径”
这是 console.log(this.selectedrecipe) https://ibb.co/bvy1Gm0的输出
配方-item.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Recipe } from 'src/app/shared/recipe.modal';
import { Recipeservice } from '../../recipe.service';
@Component({
selector: 'app-recipe-item',
templateUrl: './recipe-item.component.html',
styleUrls: ['./recipe-item.component.css']
})
export class RecipeItemComponent implements OnInit {
@Input() ele:Recipe
constructor(private reservice:Recipeservice) { }
ngOnInit() {
}
onrecipeclicked(){
this.reservice.recipeselected.emit(this.ele)
}
}
食谱.service.ts
import { Recipe } from '../shared/recipe.modal';
import { EventEmitter } from '@angular/core';
export class Recipeservice{
recipeselected=new EventEmitter<Recipe>()
recipes:Recipe[]=[
new Recipe('Big Fat Burger','What else you need to say
?','https://www.seriouseats.com/recipes/images/2015/07/20150702-
sous-vide-hamburger-anova-primary-1500x1125.jpg'),
new Recipe('Tasty Schnitzel','A super tasty burger','
https://www.seriouseats.com/recipes/images/2015/07/20150702-
sous-vide-hamburger-anova-primary-1500x1125.jpg')
]
}
配方详细信息.component.ts
import { Component, OnInit } from '@angular/core';
import { Recipeservice } from '../recipe.service';
@Component({
selector: 'app-recipe-details',
templateUrl: './recipe-details.component.html',
styleUrls: ['./recipe-details.component.css']
})
export class RecipeDetailsComponent implements OnInit {
constructor(private reservice:Recipeservice) { }
selectedrecipe
ngOnInit() {
this.reservice.recipeselected.subscribe((recipe)=>{
this.selectedrecipe=recipe
console.log(this.selectedrecipe)
})
}
}
配方详细信息.component.html
<div class="row">
<div class="col-xs-12">
<img src="{{selectedrecipe.imagepath}}" alt=" .
{{selectedrecipe.name}}" class="img-responsive" style="max-
height:300px">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h1></h1>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="btn-group">
<button class="btn btn-primary dropdown-toggle"
type="button">Manage Recipe <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a style="cursor:pointer;">Shopping List</a></li>
<li><a href="#">Edit Recipe</a></li>
<li><a href="#">Delete Recipe</a></li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<!-- Ingredients -->
<ul class="list-group">
<li class="list-group-item"
>
</li>
</ul>
</div>
</div>
解决方案
由于是selectedrecipe
异步设置的,您应该在您的模板上使用默认值@Input()
或使用模板中的保存导航运算符?.
:"
<img src="{{selectedrecipe?.imagepath}}" alt="{{selectedrecipe?.name}}">
推荐阅读
- java - 我知道happens-before并不意味着发生在之前,代码“A = B + 1; B = 1;”可以吗?产生结果“A == 2 and B == 1”?
- hyperledger-fabric - 为什么我会随机开始接收现有通道的错误“MSP 错误:通道不存在”Hyperledger Fabric?
- html - 尽管设置了安全策略,但 Google 字体未加载
- database - 如何以实用的方式将自定义数据保存在 sales_flat_creditmemo 表中 Magento 1
- ios - 使用带有 CocoaPods 1.5 的静态库,在导入时没有这样的模块
- c++ - “this”指针只是编译时的东西吗?
- c# - 在 PowerPoint.Slide 中添加 Charting.Chart
- go - 为可扩展的 Go 应用程序提供 Redis 客户端的最佳方式
- node.js - 有没有办法在没有表单或没有输入标签的情况下从浏览器将变量传递给 node.js?
- java - 具有不同类型返回的功能接口中的 Null