当我获取 API 时,只在 console.log() 中获取 URL
Only getting the URL in the console.log() when I fetch API
我在 console.log(arrayCalendar) 中得到这个,我希望它是 return JSON 数据!这个 console.log 显示像这样!这是来自 api!
{
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": true,
"value": {
"url": "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/",
"body": null,
"reportProgress": false,
"withCredentials": false,
"responseType": "json",
"method": "GET",
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"params": {
"updates": null,
"cloneFrom": null,
"encoder": {},
"map": null
},
"urlWithParams": "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/"
}
},
"operator": {
"concurrent": 1
}
},
"operator": {}
},
"operator": {}
}
我的service.ts如下:
import {Injectable} from '@angular/core';
//import { Observable } from 'tns-core-modules/ui/page/page';
//import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Color } from "tns-core-modules/color";
// >> angular-calendar-require
import { CalendarEvent } from 'nativescript-ui-calendar';
import { CalendarClass } from '../classes/calendarClass';
@Injectable()
export class calendarApiService{
listOfCalendars: CalendarClass[];
apiUrl = "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/";
constructor(private httpclient : HttpClient) {
}
getCalendarData(){
return this.httpclient.get<CalendarClass[]>(this.apiUrl);
}
getCalendarEvents(): Array<CalendarEvent> {
var arrayCalendar = this.getCalendarData();
console.log(arrayCalendar);
const appointments = [
{
"date": " 2018-09-04",
"colour": "#87CEFA",
"title": "Available - 09-04-2018 00:00 to 09-04-2018 05:59"
},
{
"date": " 2018-09-04",
"colour": "#90EE90",
"title": "Not Available - 09-04-2018 00:00 to 09-04-2018 23:59"
}
]
let startDate: Date,
endDate: Date,
event: CalendarEvent;
let colors: Array<Color> = [new Color(200, 188, 26, 214), new Color(220, 255, 109, 130), new Color(255, 55, 45, 255), new Color(199, 17, 227, 10), new Color(255, 255, 54, 3)];
let events: Array<CalendarEvent> = new Array<CalendarEvent>();
for (let i = 1; i < appointments.length; i++) {
event = new CalendarEvent(appointments[i].title, new Date(appointments[i].date), new Date(appointments[i].date), false, colors[i * 10 % (appointments[i].colour.length - 1)]);
events.push(event);
}
return events;
}
}
我希望 api 中的数据看起来像这样!实际apireturns数据在邮递员get中是这样的!
[{
"date": " 2018-09-04",
"colour": "#87CEFA",
"title": "Available - 09-04-2018 00:00 to 09-04-2018 05:59"
}, {
"date": " 2018-09-04",
"colour": "#90EE90",
"title": "Not Available - 09-04-2018 00:00 to 09-04-2018 23:59"
}, {
"date": " 2018-09-05",
"colour": "#87CEFA",
"title": "Available - 09-05-2018 00:00 to 09-05-2018 05:59"
}, {
"date": " 2018-09-06",
"colour": "#90EE90",
"title": "Not Available - 09-06-2018 00:00 to 09-06-2018 23:59"
}
]
如何从我请求的 api 中获取上述数据!当我拿到数据的时候,我可以通过上面写的for循环来迭代数据!
这是组件,
import { Component, OnInit } from '@angular/core';
import { calendarApiService } from './service/api.service';
import { CalendarClass } from './classes/calendarClass';
import { RadCalendar, CalendarEvent, CalendarSelectionEventData } from "nativescript-ui-calendar";
@Component({
selector: 'ns-calendar',
templateUrl: './calendar.component.html',
styleUrls: ['./calendar.component.css'],
moduleId: module.id,
providers: [calendarApiService]
})
export class CalendarComponent implements OnInit {
constructor(private _calendarApiService: calendarApiService) {
//console.log("hi");
}
private _events: Array<CalendarEvent>;
private _listItems: Array<CalendarEvent>;
listOfCalendar: CalendarClass[];
get eventSource() {
return this._events;
}
get myItems(): Array<CalendarEvent> {
return this._listItems;
}
set myItems(value) {
this._listItems = value;
}
// ngOnInit() {
// this._events = this._calendarService.getCalendarEvents();
// }
ngOnInit() {
this._events = this._calendarApiService.getCalendarEvents();
this._calendarApiService.getCalendarData()
.subscribe(
data=>{
this.listOfCalendar = data;
}
);
//console.log(this.listOfCalendar);
}
onDateSelected(args: CalendarSelectionEventData) {
const calendar: RadCalendar = args.object;
const date: Date = args.date;
const events: Array<CalendarEvent> = calendar.getEventsForDate(date);
this.myItems = events;
}
}
这是日历 class:
export class CalendarClass{
date: Date;
colour: string;
title: string;
}
如果我能回答您的问题,那么我想您希望在 var arrayCalendar = this.getCalendarData()
、
这一行获取数据
所以你需要订阅 Observable。
var arrayCalendar:any = []
this.getCalendarData().subscribe((data: any) => {
arrayCalendar = data;
console.log(arrayCalendar);
}
);
我在 console.log(arrayCalendar) 中得到这个,我希望它是 return JSON 数据!这个 console.log 显示像这样!这是来自 api!
{
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": true,
"value": {
"url": "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/",
"body": null,
"reportProgress": false,
"withCredentials": false,
"responseType": "json",
"method": "GET",
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"params": {
"updates": null,
"cloneFrom": null,
"encoder": {},
"map": null
},
"urlWithParams": "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/"
}
},
"operator": {
"concurrent": 1
}
},
"operator": {}
},
"operator": {}
}
我的service.ts如下:
import {Injectable} from '@angular/core';
//import { Observable } from 'tns-core-modules/ui/page/page';
//import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Color } from "tns-core-modules/color";
// >> angular-calendar-require
import { CalendarEvent } from 'nativescript-ui-calendar';
import { CalendarClass } from '../classes/calendarClass';
@Injectable()
export class calendarApiService{
listOfCalendars: CalendarClass[];
apiUrl = "https://prpresponse.co.uk/Wforce90672/getcandidatecalendardatav2.php/";
constructor(private httpclient : HttpClient) {
}
getCalendarData(){
return this.httpclient.get<CalendarClass[]>(this.apiUrl);
}
getCalendarEvents(): Array<CalendarEvent> {
var arrayCalendar = this.getCalendarData();
console.log(arrayCalendar);
const appointments = [
{
"date": " 2018-09-04",
"colour": "#87CEFA",
"title": "Available - 09-04-2018 00:00 to 09-04-2018 05:59"
},
{
"date": " 2018-09-04",
"colour": "#90EE90",
"title": "Not Available - 09-04-2018 00:00 to 09-04-2018 23:59"
}
]
let startDate: Date,
endDate: Date,
event: CalendarEvent;
let colors: Array<Color> = [new Color(200, 188, 26, 214), new Color(220, 255, 109, 130), new Color(255, 55, 45, 255), new Color(199, 17, 227, 10), new Color(255, 255, 54, 3)];
let events: Array<CalendarEvent> = new Array<CalendarEvent>();
for (let i = 1; i < appointments.length; i++) {
event = new CalendarEvent(appointments[i].title, new Date(appointments[i].date), new Date(appointments[i].date), false, colors[i * 10 % (appointments[i].colour.length - 1)]);
events.push(event);
}
return events;
}
}
我希望 api 中的数据看起来像这样!实际apireturns数据在邮递员get中是这样的!
[{
"date": " 2018-09-04",
"colour": "#87CEFA",
"title": "Available - 09-04-2018 00:00 to 09-04-2018 05:59"
}, {
"date": " 2018-09-04",
"colour": "#90EE90",
"title": "Not Available - 09-04-2018 00:00 to 09-04-2018 23:59"
}, {
"date": " 2018-09-05",
"colour": "#87CEFA",
"title": "Available - 09-05-2018 00:00 to 09-05-2018 05:59"
}, {
"date": " 2018-09-06",
"colour": "#90EE90",
"title": "Not Available - 09-06-2018 00:00 to 09-06-2018 23:59"
}
]
如何从我请求的 api 中获取上述数据!当我拿到数据的时候,我可以通过上面写的for循环来迭代数据!
这是组件,
import { Component, OnInit } from '@angular/core';
import { calendarApiService } from './service/api.service';
import { CalendarClass } from './classes/calendarClass';
import { RadCalendar, CalendarEvent, CalendarSelectionEventData } from "nativescript-ui-calendar";
@Component({
selector: 'ns-calendar',
templateUrl: './calendar.component.html',
styleUrls: ['./calendar.component.css'],
moduleId: module.id,
providers: [calendarApiService]
})
export class CalendarComponent implements OnInit {
constructor(private _calendarApiService: calendarApiService) {
//console.log("hi");
}
private _events: Array<CalendarEvent>;
private _listItems: Array<CalendarEvent>;
listOfCalendar: CalendarClass[];
get eventSource() {
return this._events;
}
get myItems(): Array<CalendarEvent> {
return this._listItems;
}
set myItems(value) {
this._listItems = value;
}
// ngOnInit() {
// this._events = this._calendarService.getCalendarEvents();
// }
ngOnInit() {
this._events = this._calendarApiService.getCalendarEvents();
this._calendarApiService.getCalendarData()
.subscribe(
data=>{
this.listOfCalendar = data;
}
);
//console.log(this.listOfCalendar);
}
onDateSelected(args: CalendarSelectionEventData) {
const calendar: RadCalendar = args.object;
const date: Date = args.date;
const events: Array<CalendarEvent> = calendar.getEventsForDate(date);
this.myItems = events;
}
}
这是日历 class:
export class CalendarClass{
date: Date;
colour: string;
title: string;
}
如果我能回答您的问题,那么我想您希望在 var arrayCalendar = this.getCalendarData()
、
所以你需要订阅 Observable。
var arrayCalendar:any = []
this.getCalendarData().subscribe((data: any) => {
arrayCalendar = data;
console.log(arrayCalendar);
}
);