【问题标题】:Can't see values in console.log() in nativescript在 nativescript 中看不到 console.log() 中的值
【发布时间】:2019-02-22 13:34:19
【问题描述】:

我有一个包含数组的 js 文件! js文件是这样的

export const appointments = [
{
  "taskID": 9018929,
  "resource_ID": 1,
  "title": "KNDL Thatcham",
  "description": "<Non-Selected>\r\n10:00 - 18:00",
  "startTime": "2018-12-28T00:00:00",
  "endTime": "2018-12-28T23:59:00",
  "statusColour": "#DFDFDF",
  "clientColour": "#FFFDD7"
}]

我已经在 service.ts 中导入了这个 js 文件。而这个约会.js 并没有显示在 console.log 中。

import { Injectable } from '@angular/core';
        import { appointments } from '../appointments';
        import { Color } from "tns-core-modules/color";
        // >> angular-calendar-require
        import { CalendarEvent } from 'nativescript-ui-calendar';


        @Injectable({
        providedIn: 'root'
        })


    export class CalendarService {

      constructor() { 
      //var a="test test etsdhsakdsakd dsddssdsd";
      console.log(appointments);
    } 

这是我要导入 service.ts 的组件

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

import { CalendarService } from "./service/calendar.service";
import { RadCalendar, CalendarEvent, CalendarSelectionEventData } from "nativescript-ui-calendar";
@Component({
  selector: 'ns-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  moduleId: module.id,
  providers: [CalendarService]
})
export class CalendarComponent implements OnInit {
}

【问题讨论】:

  • 您确定CalendarService() 正在被实例化吗?如果将console.log(appointments); 移到类定义之外(即直接在import 之后)会发生什么?
  • @DacreDenny 我已经在另一个组件中导入了 service.ts!我会在导入部分后告诉您它是否有效!再次检查代码,我已经添加了我导入service.ts的组件部分
  • 您刚才提到了providers。我认为在将 CalendarService 的实例注入组件的构造函数之前不会创建它。

标签: nativescript angular2-nativescript nativescript-angular


【解决方案1】:

您需要在构造函数中添加一个 CalendarService 类型的私有 calendarService 参数。

当 Angular 创建 CalendarComponent 时,Dependency Injection 系统将 calendarService 参数设置为 CalendarService 的单例实例。

导出类 CalendarComponent 实现 OnInit { 构造函数(日历服务:日历服务){}

}

【讨论】:

  • 喜欢这个? constructor(private _calendarApiService: calendarApiService) { }
  • 我仍然没有在终端中得到 console.log() !我查了tns医生!一切都好!但它仍然没有显示!
  • 尝试在构造函数中包含 console.log 以确保 caldendarComponent 正在启动
  • 我试过了。但它没有出现!这是我在终端设备 LGAS992174f3745 上成功同步的应用程序 org.nativescript.UnityMobile 中找到的消息。由于正在进行捆绑或实时同步,因此已跳过挂钩。 Preparing project... 项目成功准备 (Android) 成功将 api.service.js 转移到设备 LGAS992174f3745 上。重新启动设备上的应用程序 LGAS992174f3745... 设备:LGAS992174f3745 调试端口:40000
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2019-07-04
  • 1970-01-01
  • 2019-09-12
相关资源
最近更新 更多