我有一个名为“AppModule”的根模块。“AppModule”延迟加载其他几个模块,其中之一称为“BooksAndRunModule”。我有两个属于“BooksAndRunModule”的组件,它们需要共享一个我命名为“BooksAndRunService”的服务实例。我将“BooksAndRunService”声明为提供者的第一个也是唯一的地方是在“BooksAndRunModule”中。我认为通过这样做,我的两个组件可以访问同一个服务实例,但它们没有。显然,我对依赖注入的理解不足。我不希望这项服务在应用程序范围内可用,这就是为什么我只在“BooksAndRunModule”中将其声明为提供者。什么不要 t 我明白,我怎样才能做到这一点?如果您想查看我的项目中的任何其他文件,请告诉我。
应用模块:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { AuthenticationModule } from './authentication/authentication.module';
import { SharedModule } from './shared/shared.module';
import { AppComponent } from './app.component';
import { FriendService } from './friend.service';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
AuthenticationModule,
SharedModule,
],
providers: [ FriendService ],
bootstrap: [AppComponent]
})
export class AppModule { }
BooksAndRun 模块:
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { SharedModule } from '../shared/shared.module';
import { FriendService } from '../friend.service';
import { BooksAndRunCreateComponent } from './books_and_run_create.component';
import { BooksAndRunPlayComponent } from './books_and_run_play.component';
import { BooksAndRunService } from './books_and_run.service';
import { BooksAndRunRouter } from './books_and_run.router';
@NgModule({
declarations: [
BooksAndRunCreateComponent,
BooksAndRunPlayComponent,
],
imports: [
CommonModule,
SharedModule,
BooksAndRunRouter,
],
providers: [ FriendService, BooksAndRunService ],
})
export class BooksAndRunModule { }
BooksAndRunCreateComponent:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FriendList } from '../friendlist';
import { FriendService } from '../friend.service';
import { BooksAndRunService } from './books_and_run.service';
@Component({
moduleId: module.id,
selector: 'books-and-run-create',
templateUrl: './books_and_run_create.component.html',
styleUrls: ['./books_and_run_create.component.css'],
})
export class BooksAndRunCreateComponent implements OnInit {
constructor(public friendService: FriendService, private booksAndRunService: BooksAndRunService, private router: Router) { }
isRequesting: boolean;
name: string = 'Aaron';
friendList: FriendList[] = [];
players: any[] = [];
private stopRefreshing() {
this.isRequesting = false;
}
ngOnInit(): void {
this.booksAndRunService.resetPlayers();
this.isRequesting = true;
this.friendService
.getFriendList()
.subscribe(
data => this.friendList = data,
() => this.stopRefreshing(),
() => this.stopRefreshing(),
)
}
addPlayer(player): void {
this.booksAndRunService.addPlayer(player);
for(var i=0; i<this.friendList.length; i++) {
if(this.friendList[i].pk === player.pk) {
this.friendList.splice(i, 1);
}
}
this.players = this.booksAndRunService.getPlayers();
console.log("Current players are: " + this.players);
}
removePlayer(player): void {
this.booksAndRunService.removePlayer(player);
this.friendList.push(player);
this.players = this.booksAndRunService.getPlayers();
console.log("Current players are: " + this.players)
}
goToGame(): void {
console.log('Going to game with players: ' + this.booksAndRunService.getPlayers());
this.router.navigate(['/books_and_run/play'])
}
}
BooksAndRunPlay 组件:
import { Component, OnInit, AfterViewChecked } from '@angular/core';
import { BooksAndRunService } from './books_and_run.service';
import { Score } from './books_and_run.classes';
@Component({
moduleId: module.id,
selector: 'books-and-run-play',
templateUrl: './books_and_run_play.component.html',
styleUrls: ['./books_and_run_play.component.css'],
})
export class BooksAndRunPlayComponent implements OnInit, AfterViewChecked {
constructor(public booksAndRunService: BooksAndRunService) { }
game = { players: []};
ngOnInit(): void {
console.log("Initalizing BooksAndRunPlayComponent...")
console.log("Here are the players: " + this.booksAndRunService.getPlayers())
var game: any;
if(localStorage.getItem('game') === null) {
console.log("Creating a new game...");
this.game = this.booksAndRunService.prepareGame();
this.booksAndRunService.saveGame(this.game);
} else {
console.log("Restoring game from localStorage...");
this.game = this.booksAndRunService.restoreGame();
};
}
ngAfterViewChecked() {
this.booksAndRunService.saveGame(this.game);
}
}
BooksAndRunService:
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Game, Player, Score, Round } from './books_and_run.classes'
@Injectable()
export class BooksAndRunService {
players: Player[];
getPlayers() {
return this.players;
}
addPlayer(player) {
this.players.push(player);
}
removePlayer(player) {
for(var i=0; i<this.players.length; i++) {
if(this.players[i].pk === player.pk) {
this.players.splice(i, 1);
}
}
}
resetPlayers() {
this.players = [];
}
}