亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在組件之間傳遞數據不起作用 Angular

在組件之間傳遞數據不起作用 Angular

阿晨1998 2022-01-07 18:43:58
我有一個問題,我正在嘗試將playlist[]數組從 component.ts傳遞給 header.ts。ERROR TypeError: Cannot read property 'length' of undefined下面的代碼:組件.tsimport { Component, OnInit } from '@angular/core';import { ApiService } from '../../../services/api.service';import { FormGroup, FormControl } from '@angular/forms';import { FormBuilder } from '@angular/forms';import { faSearch } from '@fortawesome/free-solid-svg-icons';import { faRedo } from '@fortawesome/free-solid-svg-icons';import { faHeadphones } from '@fortawesome/free-solid-svg-icons';import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons';import { faPlus } from '@fortawesome/free-solid-svg-icons';@Component({  selector: 'app-content',  templateUrl: './content.component.html',  styleUrls: ['./content.component.scss']})export class ContentComponent {  public data = [];  public playlist = [];  public apiData: any;  public results = [];  public loading = false;  public noData: any;  p: number = 1;  faSearch = faSearch;  faRedo = faRedo;  faHeadphones = faHeadphones;  faExternalLinkAlt = faExternalLinkAlt;  faPlus = faPlus;  searchQuery: string = "";  clickMessage = '';  constructor(private service: ApiService) { }  getAll() {    this.service.getAll(this.searchQuery).subscribe((results) => {      this.loading = true;      console.log('Data is received - Result - ', results);      this.data = results.results;      this.loading = false;      if (this.data.length <= 0) {        this.noData = true;      } else if (this.data.length >= 1) {        this.noData = false;      } else {        this.noData = false;      }    })  }  closeAlert() {    this.noData = false;  }  addSongToPlaylist(itunes) {    this.playlist.push(itunes);    console.log('Playlist - ', this.playlist);}  refresh(): void {    window.location.reload();  }
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

你的問題是里面的對象playlist和開始的一樣。嘗試克隆對象并將新項目添加到克隆項目中:


import { Component, OnInit } from '@angular/core';

import { ApiService } from '../../../services/api.service';

import { FormGroup, FormControl } from '@angular/forms';

import { FormBuilder } from '@angular/forms';

import { faSearch } from '@fortawesome/free-solid-svg-icons';

import { faRedo } from '@fortawesome/free-solid-svg-icons';

import { faHeadphones } from '@fortawesome/free-solid-svg-icons';

import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons';

import { faPlus } from '@fortawesome/free-solid-svg-icons';


@Component({

  selector: 'app-content',

  templateUrl: './content.component.html',

  styleUrls: ['./content.component.scss']

})

export class ContentComponent {


  public data = [];

  public playlist = [];

  public apiData: any;

  public results = [];

  public loading = false;

  public noData: any;

  p: number = 1;

  faSearch = faSearch;

  faRedo = faRedo;

  faHeadphones = faHeadphones;

  faExternalLinkAlt = faExternalLinkAlt;

  faPlus = faPlus;


  searchQuery: string = "";

  clickMessage = '';


  constructor(private service: ApiService) { }


 ...

  addSongToPlaylist(itunes) {

    const playlistSong = {...this.playlist};

    playlistSong.push(itunes);

    this.playlist = playlistSong;

    console.log('Playlist - ', this.playlist);

  }

 ...

}


查看完整回答
反對 回復 2022-01-07
?
呼如林

TA貢獻1798條經驗 獲得超3個贊

您需要從組件輸出播放列表,然后將其分配給頁面組件中的變量,然后可以將其傳遞給應用程序標題:


<app-header [playlist]="playlist"></app-header>

<app-content [playlistOutputEvent]="playlist = $event;"></app-content>


查看完整回答
反對 回復 2022-01-07
  • 2 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號