【发布时间】:2018-01-17 14:15:51
【问题描述】:
我是 Angular 2 的新手,我对使用服务的跨组件通信的这种使用究竟如何工作表示怀疑。
在我的应用程序中,我有这个 RecipeService 服务类:
@Injectable()
export class RecipeService {
// Hold a Recipe object to be emitted to another component to implement cross component comunication:
recipeSelected = new EventEmitter<Recipe>();
// List of all recipes (maybe later can be obtained by a web service)
private recipes: Recipe[] = [
new Recipe(
'Tasty Schnitzel',
'A super-tasty Schnitzel - just awesome!',
'https://upload.wikimedia.org/wikipedia/commons/7/72/Schnitzel.JPG',
[
new Ingredient('Meat', 1),
new Ingredient('French Fries', 20)
]),
new Recipe('Big Fat Burger',
'What else you need to say?',
'https://upload.wikimedia.org/wikipedia/commons/b/be/Burger_King_Angus_Bacon_%26_Cheese_Steak_Burger.jpg',
[
new Ingredient('Buns', 2),
new Ingredient('Meat', 1)
])
];
// Inject a sub service:
constructor(private slService: ShoppingListService) {}
/**
* Return a copy of the reipes array.
* @returns {Recipe[]}
*/
getRecipes() {
return this.recipes.slice();
}
addIngredientsToShoppingList(ingredients: Ingredient[]) {
this.slService.addIngredients(ingredients);
}
}
这个类被两个不同的组件用来实现这个发射器的跨组件通信:
recipeSelected = new EventEmitter<Recipe>();
据我了解(如果我做错了断言,请纠正我)这个 recipeSelected 发出事件,该事件保存包含在 Recipe 对象中的信息(它包含一些字符串字段)。
然后我有这个 RecipeItemComponent 组件(它代表一个食谱,它查看显示与特定食谱相关的信息):
@Component({
selector: 'app-recipe-item',
templateUrl: './recipe-item.component.html',
styleUrls: ['./recipe-item.component.css']
})
export class RecipeItemComponent implements OnInit {
@Input() recipe: Recipe;
// Inkect the RecipeService to use it in this component:
constructor(private recipeService: RecipeService) { }
ngOnInit() {
}
/**
* When a specific recipe is selected in the page it emit the selected recipe to comunicate
* with another component
*/
onSelected() {
this.recipeService.recipeSelected.emit(this.recipe);
}
}
当用户点击一个链接到与这个RecipeItemComponent相关的视图时,这个类的onSelected()方法被执行。
据我所知,它只是发出与此 Recipe 对象相关的事件。所以我认为这是拍摄给别人这个对象的内容,别人应该是另一个组件(所以它实现了跨组件通信的概念)。
然后我有了另一个 RecipesComponent 组件类:
@Component({
selector: 'app-recipes',
templateUrl: './recipes.component.html',
styleUrls: ['./recipes.component.css'],
providers: [RecipeService]
})
export class RecipesComponent implements OnInit {
selectedRecipe: Recipe;
/**
* Inject the RecupeService to use it in this component
* @param recipeService
*/
constructor(private recipeService: RecipeService) { }
/**
* Subscribe on the event emitted when a recipe is selected:
*/
ngOnInit() {
this.recipeService.recipeSelected
.subscribe(
(recipe: Recipe) => {
this.selectedRecipe = recipe;
}
);
}
}
据我所知,我正在将此类事件的“侦听器”(它是侦听器吗?)注册到 ngOnInit() 方法中,通过:
ngOnInit() {
this.recipeService.recipeSelected
.subscribe(
(recipe: Recipe) => {
this.selectedRecipe = recipe;
}
);
}
因此,在实践中,每次 RecipeItemComponent 组件发出包含 Recipe 对象的事件时,RecipesComponent 都会收到此信息使用它的组件。是吗?
那我对这个sintax有疑问:
(recipe: Recipe) => {
this.selectedRecipe = recipe;
}
具体是什么意思?我认为 recipe: Recipe 是接收到的事件的内容。这有点像声明函数的隐式方式? (我来自 Java,我不太喜欢这种语法)。
另一个疑问是:为什么将这段代码声明到 ngOnInit() 中?我的想法是,它在创建此组件时声明一个侦听器,然后此侦听器对可能第二次出现的事件做出反应。是吗?
【问题讨论】:
-
如果您来自 Java,请检查 lambda 表达式,箭头函数已经存在。
标签: javascript angular javascript-framework angular-services angular-components