在TypeScript中創建一個全局變量

[英]Create a global variable in TypeScript


In JavaScript I can just do this:

在JavaScript中,我可以這樣做:

 something = 'testing';

And then in another file:

然后在另一個文件中:

 if (something === 'testing')

and it will have something be defined (as long as they were called in the correct order).

它將有一些東西被定義(只要它們以正確的順序被調用)。

I can't seem to figure out how to do that in TypeScript.

我似乎無法弄清楚如何在TypeScript中做到這一點。

This is what I have tried.

這就是我的嘗試。

In a .d.ts file:

在.d.ts文件中:

interface Window { something: string; }

Then in my main.ts file:

然后在我的main.ts文件中:

 window.something = 'testing';

then in another file:

然后在另一個文件中:

 if (window.something  === 'testing')

And this works. But I want to be able to lose the window. part of it and just have my something be global. Is there a way to do that in TypeScript?

這很有效。但我希望能夠失去窗口。它的一部分,讓我的東西成為全球性的。有沒有辦法在TypeScript中做到這一點?

(In case someone is interested, I am really trying to setup my logging for my application. I want to be able to call log.Debug from any file without having to import and create objects.)

(如果有人感興趣,我真的想為我的應用程序設置日志記錄。我希望能夠從任何文件調用log.Debug而無需導入和創建對象。)

5 个解决方案

#1


6  

Okay, so this is probably even uglier that what you did, but anyway...

好吧,所以你做的事情可能更加丑陋,但無論如何......

but I do the same so...

但我這樣做......

What you can do to do it in pure TypeScript, is to use the eval function like so :

在純TypeScript中可以做的是使用eval函數,如下所示:

declare var something: string;
eval("something = 'testing';")

And later you'll be able to do

以后你就能做到

if (something === 'testing')

This is nothing more than a hack to force executing the instruction without TypeScript refusing to compile, and we declare var for TypeScript to compile the rest of the code.

這只不過是在沒有TypeScript拒絕編譯的情況下強制執行指令的黑客攻擊,我們為TypeScript聲明了var來編譯其余的代碼。

#2


13  

Inside a .d.ts definition file

在.d.ts定義文件中

type MyGlobalFunctionType = (name: string) => void

If you work in the browser, you add members to the browser's window context:

如果您在瀏覽器中工作,則將成員添加到瀏覽器的窗口上下文中:

interface Window {
  myGlobalFunction: MyGlobalFunctionType
}

Same idea for NodeJS:

NodeJS的想法一致:

declare module NodeJS {
  interface Global {
    myGlobalFunction: MyGlobalFunctionType
  }
}

Now you declare the root variable (that will actually live on window or global)

現在你聲明根變量(它實際上將存在於窗口或全局)

declare const myGlobalFunction: MyGlobalFunctionType;

Then in a regular .ts file, but imported as side-effect, you actually implement it:

然后在常規.ts文件中,但作為副作用導入,實際上是實現它:

global/* or window */.myGlobalFunction = function (name: string) {
  console.log("Hey !", name);
};

And finally use it elsewhere in the codebase, with either:

最后在代碼庫的其他地方使用它,使用以下任一方法:

global/* or window */.myGlobalFunction("Kevin");

myGlobalFunction("Kevin");

#3


8  

I found a way that works if I use JavaScript combined with TypeScript.

我找到了一種方法,如果我使用JavaScript結合TypeScript。

logging.d.ts:

logging.d.ts:

declare var log: log4javascript.Logger;

log-declaration.js:

登錄declaration.js:

log = null;

initalize-app.ts

initalize-app.ts

import './log-declaration.js';

// Call stuff to actually setup log.  
// Similar to this:
log = functionToSetupLog();

This puts it in the global scope and TypeScript knows about it. So I can use it in all my files.

這使它成為全局范圍,TypeScript知道它。所以我可以在我的所有文件中使用它。

NOTE: I think this only works because I have the allowJs TypeScript option set to true.

注意:我認為這只能起作用,因為我將allowJs TypeScript選項設置為true。

If someone posts an pure TypeScript solution, I will accept that.

如果有人發布純TypeScript解決方案,我會接受。

#4


4  

im using only this

我只使用這個

import {globalVar} from "./globals";
declare let window:any;
window.globalVar = globalVar;

#5


0  

As an addon to Dima V's answer this is what I did to make this work for me.

作為Dima V答案的補充,這就是我為此做的工作。

// First declare the window global outside the class

//首先在類外聲明全局窗口

declare let window: any;

聲明讓窗口:任何;

// Inside the required class method

//在所需的類方法中

let globVarName = window.globVarName;

let globVarName = window.globVarName;


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/08/11/24710d9f53da1bd5afda54195ed1ede7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com