typescript教程阮一峰_在博图里怎样定义一个变量

typescript教程阮一峰_在博图里怎样定义一个变量在TypeScript项目中像PHP一样使用魔术变量

大家好,又见面了,我是你们的朋友全栈君。

PHP 当中有许多很有用的魔术变量, 比如__CLASS__, __METHOD__之类. 但是typescript中并没有. 因此我写了一个插件typescript-magic-variable-plugin来使用它们, 源代码已经放到了GitHub上: https://github.com/acrazing/t….

使用方法

  1. 首先你需要安装这个包: npm install -D typescript-magic-variable-plugin
  2. 修改一下你的tsconfig:

    {
        "compilerOptions": {
            "plugins": [
                { "transform": "typescript-magic-variable-plugin" }
            ]
        },
        "include": [
            // ...
            "./node_modules/typescript/magic-variable-plugin/types/globals.d.ts"
        ]
    }
  3. 在代码中使用魔术变量, 比如:

    export class Foo {
        constructor() {
            console.log(__CLASS__)
        }
    }
  4. ttypescript来编译你的项目, 注意这里不能用typescript, 因为没有开放transform接口, 需要全局安装一下ttypescript: npm i -g ttypescript, 然后调用ttsc来编译.

进阶

  1. 也可以在webpack中使用:

    const { createMagicVariableTransformer } = require('typescript-magic-variable-plugin')
    // ...
    rules: [
        {
            test: /\.tsx?$/,
            loader: 'awesome-typescript-loader',
            options: {
                // ... other loader's options
                getCustomTransformers: program => ({
                    before: [
                       createMagicVariableTransformer(program, {})
                    ]
                })
            }
        }
    ]
  2. 目前支持的变量:

    name type description
    __NAMESPACE__ string the full name of the current namespace, use . to join the parents
    __CLASS__ string the class name of the declaring class
    __METHOD__ string the method name of the declaring method
    __FUNCTION__ string the function name of the declaring function
    __DIR__ string the current directory of the code
    __FILE__ string the current file full name of the code
    __LINE__ number the current line number
  3. 可以自动给React的组件添加displayName属性, 默认开启, 比如:

    export class Foo extends Component {}

    会自动给Foo增加静态属性: Foo.displayName = "Foo"

  4. 配置:

    interface Options {
        addDisplayName: boolean; // 是否给react组件添加displayName属性, 默认开启
        rootDir: string; // __DIR__的相对路径, 默认为tscofnig.json中的rootDir或者当前文件夹
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107352.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IOCP使用acceptEX进行异步接收

    IOCP使用acceptEX进行异步接收示例代码#include<winsock2.h>#include<windows.h>#include<string>#include<iostream>#include<process.h>#include<ws2tcpip.h>#include<mswsock.h>usingnamespacestd;//#pragmacomment(lib,”MSWSOCK.lib”)#pragm

    2022年9月29日
    0
  • Kafka 是什么?

    Kafka 是什么?前言本文隶属于专栏《1000个问题搞定大数据技术体系》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!本专栏目录结构和参考文献请见1000个问题搞定大数据技术体系正文Kafka的诞生背景2011年年初,美国领英公司(Linkedin)开源了一款基础架构软件,以奥地利作家弗兰兹・卡夫卡(FranzKafka)的名字命名。之后Linkedin将其贡献给Apache基金会,随后该软件于2012年10月成功完成孵化并顺利晋升为Apache

    2022年10月13日
    0
  • eclipse方法自动注释_eclipse快速补全

    eclipse方法自动注释_eclipse快速补全1、Eclipse自动补全功能设置,默认是键入“.”才会有代码提示,否则就只有按“Alt+/”组合键。通过下面的设置可以按照你自己的需求显示代码提示。1)、直接设置打开Eclipse->Window->Perferences->Java->Editor->ContentAssist,右边出现的选项中,有一个AutoactivationtriggersorforJava

    2022年10月9日
    0
  • pycharm中pyqt5使用方法_python环境变量的配置

    pycharm中pyqt5使用方法_python环境变量的配置环境:window10;pycharm;python;博客讲述如何配置UI设计工具以及ui文件转py文件的uic工具

    2022年8月28日
    0
  • Android9.0_P:ClassNotFoundException:Didn’t find class “org.apache.http.ProtocolVersion” on path:

    Android9.0_P:ClassNotFoundException:Didn’t find class “org.apache.http.ProtocolVersion” on path:

    2021年9月30日
    36
  • 在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包

    在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包Python有一个全球社区:在这里,我们可以搜索Python第三方库的任何话题。PyPI的全称是Python包指数指Python包的指数。它是由PSF(Python软件基金会)和显示全球Python计算生态系统。我们需要学会使用PyPI的主要网站,搜索和发现我们使用第三方Python库和关心。例如,如果您正在开发一个blockchain-related程序,您需要使用Python的计算生态三个步…

    2022年10月14日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号