GVKun编程网logo

如何像使用Javascript一样访问Typescript中的“ this”?(typescript调用js)

15

以上就是给各位分享如何像使用Javascript一样访问Typescript中的“this”?,其中也会对typescript调用js进行解释,同时本文还将给你拓展Angular2typescript

以上就是给各位分享如何像使用Javascript一样访问Typescript中的“ this”?,其中也会对typescript调用js进行解释,同时本文还将给你拓展Angular 2 typescript调用javascript函数、JavaScript Vs Typescript中的类、javascript – React JS / Typescript中的空结合运算符、javascript – TypeScript中的嵌套对象等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何像使用Javascript一样访问Typescript中的“ this”?(typescript调用js)

如何像使用Javascript一样访问Typescript中的“ this”?(typescript调用js)

这是因为默认情况下,打字稿中使用了“严格使用”。

如果您在js中尝试此操作,则会返回相同的未定义

'use strict';

function m() {
  console.log(this)
}

m()
,

问题是,当您使用TS时,您正在严格模式下运行,并且在严格模式下,函数的this未定义。

因此,对于严格模式功能,未将指定的功能装箱 一个对象,如果未指定,则将是不确定的:

source

,

就像其他答案一样,这是因为打字稿use strict。为了拥有this上下文,可以(但不应)使用new关键字。

playground

function m() {
  // @ts-ignore
  console.log(this);
}

// @ts-ignore
new m();
,

您可以这样尝试

const that = this;

function m() {
  console.log(that);
}

m();

Angular 2 typescript调用javascript函数

Angular 2 typescript调用javascript函数

是否有正确的方法从Angular 2(TypeScript)中的组件调用 JavaScript函数?

这是我的组件:

import { ElementRef,AfterViewInit }       from '@angular/core';

export class AppComponent implements AfterViewInit {

    constructor(private _elementRef: ElementRef) {
    }

    ngAfterViewInit() {
        /**
         * Works but i have this error :
         * src/app.component.ts(68,9): error TS2304: Cannot find name 'MYTHEME'.
         * src/app.component.ts(69,9): error TS2304: Cannot find name 'MYTHEME'.
         */
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();

        /**
         * Works without error,but doesn't seem like a right way to do it
         */
        var s = document.createElement("script");
        s.text = "MYTHEME.documentOnLoad.init(); MYTHEME.documentOnReady.init();";
        this._elementRef.nativeElement.appendChild(s);
    }
}

直接调用JavaScript函数会导致编译错误,但“已编译”的JavaScript文件(app.component.js)中的语法是正确的:

AppComponent.prototype.ngAfterViewInit = function () {
    MYTHEME.documentOnLoad.init();
    MYTHEME.documentOnReady.init();
};

第二种方式(appendChild)工作没有错误,但我不认为(从typescript / angular更改DOM)是正确的方法.

我发现了这个:Using a Javascript Function from Typescript我尝试声明界面:

interface MYTHEME {
    documentOnLoad: Function;
    documentOnReady: Function;
}

但是TypeScript似乎没有识别它(接口声明中没有错误).

谢谢

编辑:

在Juan Mendes的回答之后,这就是我的结局:

import { AfterViewInit }       from '@angular/core';

interface MYTHEME {
    documentOnLoad: INIT;
    documentOnReady: INIT;
}
interface INIT {
    init: Function;
}
declare var MYTHEME: MYTHEME;

export class AppComponent implements AfterViewInit {

    constructor() {
    }

    ngAfterViewInit() {
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();
    }
}
您必须使用declare告诉TypeScript有关外部(JavaScript)声明的信息.见 https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html
interface MyTheme {
    documentOnLoad: Function;
    documentOnReady: Function;
}
declare var MYTHEME: MyTheme;

或者匿名

declare var MYTHEME: {documentOnLoad: Function,documentOnReady: Function};

JavaScript Vs Typescript中的类

JavaScript Vs Typescript中的类

我在这篇ECMAScript page上读到“class”是JavaScript的一部分.在这个关于 TypeScript的页面上,我看到’class’也可以在Typescript中找到.

我的问题是,开发未来JavaScript应用程序的正确方法是利用(a)JavaScript中的面向对象功能以及EMACScript 7.0中可用的功能或(b)使用TypeScript等库

解决方法

My question is,the right approach to develop JavaScript applications FOR FUTURE is to take advantage of (a) the Object Oriented features in JavaScript and the features that will be availablein EMACScript 7.0 or (b) use libraries like TypeScript

要么没事.取决于您是否想要类型安全.

如果没有像babeljs这样的东西. https://babeljs.io/

如果是,则使用typescript http://www.typescriptlang.org/

javascript – React JS / Typescript中的空结合运算符

javascript – React JS / Typescript中的空结合运算符

参见英文答案 > Typescript the safe navigation operator ( ?. ) or (!.) and null property paths                                    4个
>            Replacement of Elvis Operator of Angular2 in Typescript                                    2个
我们在.NET中有Null合并运算符,我们可以使用如下

string postal_code = address?.postal_code;

我们可以在React JS中做同样的事情吗?

我发现我们可以用&&amp ;;操作者

在address.ts文件中

string postal_code = address && address.postal_code;

我需要什么样的.net功能可以在typescript中使用react JS,这可能吗?

就像是:

string postal_code = address?.postal_code // I am getting the error in this line if I try to use like .NET
最佳答案
这是TypeScript中提出的功能,在传奇的Issue #16下

在此功能的ECMAScript规范确定之前,它不会被引入到TypeScript中,因为TypeScript实现需要遵循该规范 – 因此您可以尽早获得它,但在这种情况下不会很早.

它被称为以下任何一种:

>空传播运算符
>存在运算符
> Null Coalesce Operator

javascript – TypeScript中的嵌套对象

javascript – TypeScript中的嵌套对象

如何在TypeScript中声明像嵌套对象的 JavaScript?

let endpoints = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

以下不起作用:

private endpoints: Object = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

抛出:

错误TS2339:“对象”类型上不存在属性“auth”.

解决方法

您可以使用接口:

interface EndpointAuth {
    login: string;
}

interface Endpoint {
    auth: EndpointAuth;
}

let endpoints: Endpoint = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

(code in playground)

您还可以使用类型而不是接口:

type EndpointAuth = {
    login: string;
}

type Endpoint = {
    auth: EndpointAuth;
}

(code in playground)

或“内联”:

let endpoints: { auth: { login: string } } = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

你当然可以把它们结合起来.

编辑

正如您想要的答案解释为什么它不能与Object一起使用:

将变量定义为Object类型(在大多数情况下)并不是您真正想要做的,通常您的意思是任何,因为:

var endpoints2: any = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

不会失败(就像你没有指定类型那样不会失败).
将变量定义为Object与将其定义为{}这是一个空对象相同,而这通常不是您所追求的,并且它仅适用于以下内容:

let o1: Object = {};
let o2: Object = Object.create(null);

但是使用any并没有太多帮助你,因为那时你基本上告诉编译器不要打扰类型安全,它会让你用变量做任何事情而不让你知道有错误:

let o: any = { x: 3,y: 6 };
console.log(o.z.toString());

在编译时不会失败但在运行时会失败:

Uncaught TypeError: Cannot read property ‘toString’ of undefined

这将在编译时失败:

let o: { x: number,y: number } = { x: 3,y: 6 };
console.log(o.z.toString());

今天关于如何像使用Javascript一样访问Typescript中的“ this”?typescript调用js的介绍到此结束,谢谢您的阅读,有关Angular 2 typescript调用javascript函数、JavaScript Vs Typescript中的类、javascript – React JS / Typescript中的空结合运算符、javascript – TypeScript中的嵌套对象等更多相关知识的信息可以在本站进行查询。

本文标签: