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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • phpstorm 2022.01.21激活【最新永久激活】[通俗易懂]

    (phpstorm 2022.01.21激活)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月30日
    304
  • Linux正确删除软连接[通俗易懂]

    在Linux上删除软连接不要使用rm-rf!!!在Linux上删除软连接不要使用rm-rf!!!在Linux上删除软连接不要使用rm-rf!!!因为如果使用rm-rflinkName的方式,如果不小心在目录后面加了“/”,或者按了Tab键补全,执行之后会删除源目录文件。如果要使用rm-rflinkName的时候一定要注意源、目标文件或目录都不要在后面加…

    2022年4月14日
    193
  • 算法6-1:哈希函数

    算法6-1:哈希函数

    2022年1月26日
    41
  • android 常用加密,分享一下Android各种类型的加密

    android 常用加密,分享一下Android各种类型的加密以下内容仅供参考具体情况以实际为准360加固assets/.appkeyassets/libjiagu.so爱加密lib/armeabi/libexecmain.soassets/ijiami.ajmassets/af.binassets/signed.binassets/ijm_lib/armeabi/libexec.soassets/ijm_lib/X86/libexec.so几维安全asse…

    2022年5月16日
    49
  • 【织梦插件】xenu软件-网站url和死链提取工具免费下载[通俗易懂]

    【织梦插件】xenu软件-网站url和死链提取工具免费下载[通俗易懂]软件名称 xenu 软件作用 网站网址提取工具 适宜人群 SEO 网址 http://www.jingdouwang.cn/zygx/wzcj/278.html 软件简介:  XenuLinkSleuth可能是你所见功能最强大的网站死链接查询的软件了。使用方法简单,仅需要输入网站URL就可以完成死链查询。用户可直接查看死链和火炼,…

    2022年7月22日
    13
  • title的中文_上什么下仿

    title的中文_上什么下仿VeryCD主页上的标题上总有一句名言,且是每次刷新变换的,看了一下代码,是用Math.floor(Math.random()*n.length);来实现的,verycd代码中源引的文件是:http://www.verycd.com/statics/title.saying但我所使用的是Windowsserver主机,所以要引用的是js文件,我的代码在这里:http://www.lidec

    2022年8月10日
    8

发表回复

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

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