加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular4 Observable to Array

发布时间:2020-12-17 18:10:48 所属栏目:安全 来源:网络整理
导读:我需要从Observable对象中获取一些数据以便在SEO中使用(更改元标题和 description). 我通过HTTP从API获取数据.数据在Observable对象中获得. 我通过订阅this.radio $以某种方式成功转换Observable对象,但这会导致函数getRadioData(slug:string)的双重请求.
我需要从Observable对象中获取一些数据以便在SEO中使用(更改元标题和&& description).

我通过HTTP从API获取数据.数据在Observable对象中获得.

我通过订阅this.radio $以某种方式成功转换Observable对象,但这会导致函数getRadioData(slug:string)的双重请求.

可能我需要将Observable对象转换为Array.

radio-details.component.ts(这里我想获得SEO的meta_title&& meta_description变量)

import { Component,OnInit } from '@angular/core';
import { RadioDetails,RadioService } from './../services/radio.service';
import { Router,ActivatedRoute,ParamMap } from '@angular/router';
import { Observable } from 'rxjs/Observable';


@Component({
  selector: 'app-radio-details',templateUrl: './radio-details.component.html',styleUrls: ['./radio-details.component.css'],providers: [RadioService]    
})

export class RadioDetailsComponent implements OnInit {
  radio$: Observable<RadioDetails[]>;
  
  constructor(
    private route: ActivatedRoute,private router: Router,private service: RadioService    
  ) { }

  ngOnInit() {

this.route.paramMap
.switchMap((params: ParamMap) =>
  this.service.getRadioData(params.get('slug'))
)
.subscribe(
  (data)  => {
    this.radio$= data;
    console.log("this.radio$IS: ",this.radio$)
    // HERE I WANT TO get meta_title && meta_description variables for SEO
    // this.radio$looks like: Object { _isScalar: false,source: Object,operator: Object } 
  }
);   
  }
}

radio.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

export class Categories{
  constructor(
    public title: string,public items: Radio[]
  ){}
}

export class Radio{
  constructor(
    public title: string,public slug: string,public external_url?: string,public isplay?: string,public css_class?: string    
  ){}
}

export class RadioDetails{
  constructor(
    public title: string,public player_type?: string,public stream?: string,public meta_title?: string,public meta_description?: string
  ){}
}

@Injectable()
export class RadioService {
  constructor(private _http: Http) { }

  getAllRadiosData(){
    return this._http.get('http://api.2net.co.il/radio/stations/all_stations.php')
    .map(res => res.json())
  }

  getRadioData(slug: string){
if (slug !== null && typeof slug !== 'undefined' && slug){
      return [
    this._http.get('http://api.2net.co.il/radio/stations/station.php?slug='+slug)
    .map(res => res.json())
  ];
}
  }
}

无线电details.component.html

<article class="page page-radio-detail">
  
      <div *ngIf="radio$| async as radio; else noRadioFound">
          <div class="playerZone">
              <header class="entry-header">
                  <h1 class="entry-title">
                      <span class="text">
                      Playing now:
                      </span>
                      <span class="radio_title">{{ radio.title }}</span>
                 </h1>
              </header>
              <div class="player-wrapper">
                      <app-radio-player stream="{{radio.stream}}" player_type="{{radio.player_type}}"></app-radio-player>
              </div>
          </div>          
      </div><!-- /ngIf -->
  
      <ng-template #noRadioFound>
          <div class="playerZone noRadioFound">
              <header class="entry-header">
                  <h1 class="entry-title">
                      <span class="text">
                      Select radio station:
                      </span>
                  </h1>
              </header>
              <div class="player-wrapper">
              click on links below:
              </div>
          </div>            
      </ng-template>

      <div class="entry-content">
          <app-radio-categories></app-radio-categories>              
    </div>
</article>

解决方法

在你们帮助我之后,解决方案是:

1.
在radio.service.ts中,不需要在函数getRadioData(slug:string)中返回Array.
正确的功能代码必须是:

getRadioData(slug: string){
    if (slug !== null && typeof slug !== 'undefined' && slug){
      return this._http.get('http://api.2net.co.il/radio/stations/station.php?slug='+slug)
      .map(res => res.json());
    }
  }

2.
radio-details.component.html中radio $的实现必须没有管道(|).
正确的部分代码必须是:

<div *ngIf="radio$as radio; else noRadioFound">
...
</div>

>毕竟在radio-details.component.ts中我得到了简单易读的对象,如{mate_description:“some meta description”,meta_title:“some_meta_title”,stream:“http://example.com”}

ngOnInit() {
    
    this.route.paramMap
    .switchMap((params: ParamMap) =>
      this.service.getRadioData(params.get('slug'))
    )
    .subscribe(
      (data)  => {
        this.radio$= data;
        console.log("this.radio$IS: ",this.radio$)
        // this.radio$- is a readable Object
      }
    );   
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读