【问题标题】:TypeScript: How can I access a variable from one scipt in another?TypeScript:如何从另一个脚本中的一个脚本访问变量?
【发布时间】:2021-04-10 12:14:42
【问题描述】:

我已经 fork 了一个 VSCode 扩展,并想对其进行修改以获得一些经验。

我想做的改变包括从this script(#1) 访问104 行的变量word 并在this script(#2) 中使用它。

我相信这可能涉及将 #1 导入到 #2 中,这就是我目前所拥有的......

import { ConfigKey, Global } from '../common/global';
import { Process } from '../common/processWrapper';
import { AhkHoverProvider } from '../providers/ahkHoverProvider';

export class HelpService {
    public static open(): void {
        var command = word; // access 'word' from AhkHoverProvider
        const helpPath = Global.getConfig(ConfigKey.helpPath);
        const commandPath = command ? '::/docs/commands/' + command + '.htm' : '';
        // Process.exec(`C:/Windows/hh.exe ${helpPath}`);
        Process.exec(`C:/Windows/hh.exe ms-its:` + helpPath + commandPath);
    }
}

我尝试阅读 TypeScript 类和导出/导入的示例,但不知道如何进行。

【问题讨论】:

    标签: typescript vscode-extensions


    【解决方案1】:

    您只能在 HoverProvider 中获取“单词”,因为它取决于光标的位置(文档和位置)。 你可以在vscode api docs找到更多信息

    您的入口点是AhkHoverProvider.provideHoverL38,您可以从这里开始向您的服务传递信息。

    
    interface Context {
        /** The character after the word */
        charAfter: string;
        word: string;
    }
    
    export class AhkHoverProvider implements HoverProvider {
    
        public async provideHover(
            document: TextDocument,
            position: Position,
            token: CancellationToken,
        ) {
            const context = this.buildContext(document, position);
            // context.word is accessible here
            HelpService.openOnCommand(context.word);
        }
    }
    

    HelpService.ts

    import { ConfigKey, Global } from '../common/global';
    import { Process } from '../common/processWrapper';
    
    export class HelpService {
        public static open(): void {
            const helpPath = Global.getConfig(ConfigKey.helpPath);
            Process.exec(`C:/Windows/hh.exe ${helpPath}`);
        }
    
        public static openOnCommand(command: string): void {
            const helpPath = Global.getConfig(ConfigKey.helpPath);
            const commandPath = `::/docs/commands/${command}.htm`;
            Process.exec(`C:/Windows/hh.exe ms-its:${helpPath}${commandPath}`);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-02
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      相关资源
      最近更新 更多