首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在角2应用程序中使用LogEntries (TypeScript)

如何在角2应用程序中使用LogEntries (TypeScript)
EN

Stack Overflow用户
提问于 2016-10-04 20:55:49
回答 2查看 698关注 0票数 0

我有这个tsconfig.json

代码语言:javascript
复制
{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "outDir": "dist"
    },
    "exclude": [
        "node_modules"
    ]
}

dashboard.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';

import Logger = require('le_node');
var log = new Logger({
  token:'abcdef-1234-ghijklm-5678-nopqrstuvwxyz'
});

@Component({
    selector: 'dashboard',
    templateUrl: 'app/dashboard.component.html'
})

export class DashboardComponent implements OnInit {

    constructor() { }

    ngOnInit(): void {
        log.log('Test LogEntries...');
    }

}

并运行npm install le_node --save来安装LogEntries。当尝试使用npm start,运行应用程序时,我会得到以下错误:

代码语言:javascript
复制
> sample-app@2.0.0 start /Users/user/Workspace/sample-app
> tsc && concurrently "tsc -w" "lite-server" 

app/dashboard.component.ts(10,25): error TS2307: Cannot find module 'le_node'.
EN

回答 2

Stack Overflow用户

发布于 2016-10-05 04:21:15

错误TS2307:找不到模块'le_node‘。

您只安装了模块,没有安装它的类型定义。如果模块不是用TypeScript编写的(例如,角),则需要从外部获取它的类型。这就是.d.ts文件的用途。

修复

在文件globals.d.ts中快速修正:

代码语言:javascript
复制
declare module "le_node";

更多

覆盖于此:https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

票数 0
EN

Stack Overflow用户

发布于 2017-04-01 09:11:40

一个对我有用的解决方案就是不要使用le_node模块,因为它对浏览器不友好。并使用日志条目REST

代码语言:javascript
复制
@Injectable()
export class LoggerService {
    constructor(private http: Http) {
    }

    public log(level: string, name: string) {
        // https://docs.logentries.com/docs/http-post
        const LOGENTRIES_URL = "https://webhook.logentries.com/noformat/logs/";
        let headers = new Headers();
        headers.append("Content-Type", 'application/json');
        let requestoptions = new RequestOptions({
            method: RequestMethod.Post,
            headers: headers,
            body: JSON.stringify({ "level": level, "message": message })
        });
        this.http.request(LOGENTRIES_URL + YOUR_LE_TOKEN, requestoptions).toPromise().catch((error) => {
            console.log("faield to send log to the logentries server");
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39861593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档