第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在組件之間傳遞數(shù)據(jù)不起作用 Angular

在組件之間傳遞數(shù)據(jù)不起作用 Angular

阿晨1998 2022-01-07 18:43:58
我有一個(gè)問題,我正在嘗試將playlist[]數(shù)組從 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貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

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


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);

  }

 ...

}


查看完整回答
反對(duì) 回復(fù) 2022-01-07
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

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


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

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


查看完整回答
反對(duì) 回復(fù) 2022-01-07
  • 2 回答
  • 0 關(guān)注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)