我有一个服务,其中包含一些像这样存储的数据以及将数据推送到它的函数:
service.ts
import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
import { SongsComponent } from './songs/songs.component';
import { Router } from '@angular/router';
import { SongsService } from './songs.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: []
})
export class AppComponent {
albums:any = [];
constructor(private songsService: SongsService){
};
ngOnInit() {
this.songsService.init()
}
}
我已将 app.component 设置为运行init函数作为种子:
const appRoutes: Routes =[
{path: '', component: AlbumsComponent},
{path: ':album', component: SongsComponent},
];
@NgModule({
declarations: [
SongsComponent,
AppComponent,
AlbumsComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes),
HttpModule
],
providers: [SongsService],
bootstrap: [AppComponent]
})
export class AppModule {}
App.module
@Component({
selector: 'app-albums',
templateUrl: './albums.component.html',
styleUrls: ['./albums.component.css'],
providers: []
})
export class AlbumsComponent {
@ViewChild('albumInput') albumInput: ElementRef;
@ViewChild('deleteIcon') deleteIcon: ElementRef;
private albums:{name: string, songs:Array<string>}[] = [];
private subscription: Subscription;
constructor(private songsService: SongsService,
private router: Router){};
ngOnInit() {
this.albums = this.songsService.albums;
this.subscription = this.songsService.albumsChanged
.subscribe(
(albums:{name: string, songs:Array<string>}[]) => {
this.albums = albums;
});
console.log(this.albums);
}
albumslog(){
console.log(this.albums)
}
// albumSongs(){
// this.router.navigate([])
// }
deleteAlbum(indexOfAlbum){
console.log(this);
console.log(indexOfAlbum);
this.songsService.albums.splice(indexOfAlbum,1);
addAlbum(albumName){
this.songsService.newAlbum(albumName);
console.log(this.songsService.albums);
当我在下一个组件中的console.log(this.albums)时,它只有我在service.ts上硬编码的种子。
Albums.component.ts
@Component({
selector: 'app-songs',
templateUrl: './songs.component.html',
styleUrls: ['./songs.component.css'],
providers: []
})
export class SongsComponent implements OnInit {
private subscription: Subscription;
private albums:any;
private album:any;
private songs:Array<string>;
private albumIndex:string;
constructor(private songsService: SongsService,
private router: Router,
private route: ActivatedRoute){};
ngOnInit() {
var albumIndex = +this.route.snapshot.params['album'];
console.log(albumIndex);
this.albums = this.songsService.albums;
this.subscription = this.songsService.albumsChanged
.subscribe(
(albums:{name: string, songs:Array<string>}[]) => {
this.albums = albums;
this.songs = this.getAlbum(albumIndex);
});
console.log(this.albums);
this.getAlbum(albumIndex);
// console.log(this.songsService.getAlbum("Así en el Cielo Como en la Selva"))
console.log(this.songs);
}
getAlbum(index: number){
return this.album = this.albums.splice(index)
songs.components :
service.albums
因此,当我转到另一条路线时,我的服务中的if(cell.getStringCellValue().contains(",")){
if (cell.getStringCellValue().contains("\"")){
String t= "\""+cell.getStringCellValue()+"\"";
data.append(t+",");
}
else{
data.append("\""+cell.getStringCellValue()+"\""+",");
}
}
为空,我如何将这些数据保存在多条路线上?