gpt4 book ai didi

Angular4 可观察到数组

转载 作者:太空狗 更新时间:2023-10-29 17:39:11 24 4
gpt4 key购买 nike

我需要从 Observable 对象中获取一些数据以用于 SEO(更改元标题和描述)。

我通过 HTTP 从 API 获取数据。数据进入 Observable 对象。

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

可能我需要将 Observable 对象转换为数组。

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())
];
}
}
}

radio-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) 不需要返回数组。正确的函数代码必须是:

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>

  1. 毕竟在 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
}
);
}

关于Angular4 可观察到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com