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)
上一篇 2022年4月20日 下午1:40
下一篇 2022年4月20日 下午1:40


相关推荐

  • ubuntu查看cuda和cudnn版本_cuda9对应的显卡版本

    ubuntu查看cuda和cudnn版本_cuda9对应的显卡版本1、查看cuda版本原来的老办法是这样的,cat/usr/local/cuda/version.txt在我的jetsonTX2上的和PC上是一样的,~$cat/usr/local/cuda/version.txtCUDAVersion10.2.3002、查看cudnn版本在PC上还是原来的老办法,cat/usr/local/cuda/include/cudnn.h|grepCUDNN_MAJOR-A2在我的JetsonTX2上不同,发现在cudn

    2022年10月8日
    3
  • 一次完整的渗透测试流程

    一次完整的渗透测试流程目录渗透测试信息收集漏洞探测漏洞利用内网转发内网渗透痕迹清除撰写渗透测试保告渗透测试渗透测试就是利用我们所掌握的渗透知识,对网站进行一步一步的渗透,发现其中存在的漏洞和隐藏的风险,然后撰写一篇测试报告,提供给我们的客户。客户根据我们撰写的测试报告,对网站进行漏洞修补,以防止黑客的入侵!渗透测试的前提是我们得经过用户的授权,才可以对网站进行渗透。如果我…

    2022年5月2日
    39
  • bwapp详细教程_bwapp之sql注入

    bwapp详细教程_bwapp之sql注入**HTMLInjection-Reflected(GET)**low:低级漏洞中,输入数据没有做校验Firstname:<script>alert(‘xss’)</script>Lastname:<script>alert(‘xss’)</script>出现xss即为成功。medium和low一样,但发现被全部显示抓包,会发现符号<>均被编码0,1,2分别对应三个等级找见对应的/bWAPP/bw

    2026年2月25日
    3
  • latex缩进与对齐_latex 换行缩进

    latex缩进与对齐_latex 换行缩进LATEX模板(中国运筹学会年会论文模板)%%Paper…关键词位于摘要下方,行首不缩进。摘要使用小五号(…以上这些词后均不换行。中文关键词之间以中文分号……2基础知识4Latex讲义1.单词之间用一个或多个空格分开.多个空格和一个空格效果相同.2.换行:生成的文件会自动换行,在tex文件中用一个回车换行……3.LaTeX在使用体验方面…

    2022年5月14日
    101
  • 千问入局,OPENAI搬出全家桶:2026,AI硬件是离钱最近的地方

    千问入局,OPENAI搬出全家桶:2026,AI硬件是离钱最近的地方

    2026年3月13日
    1
  • 最强编程模型Claude Opus 4.1上线:附保姆级使用教程

    最强编程模型Claude Opus 4.1上线:附保姆级使用教程

    2026年3月16日
    3

发表回复

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

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