Access javascript variable included in HTML header from typescript

我只是一个虾纸丫 提交于 2021-02-11 10:16:27

问题


I have a javascript file included in the <head></head> tags of my Angular 2 app. I would like to access a function in that javascript file from my typescript file, how should I do that? When I'm running the app, I can type var simplemde = new SimpleMDE() in the browser console to get my markdown editor working, but how can I do this in the constructor in my typescript file?


回答1:


Just declare it at the top of your component or wherever you need it like so:

declare var SimpleMDE: any;

Then you can call it in your constructor like you want!




回答2:


You can use namespacing, which is basically an object and it wraps all other variable & function

Like this

var SomeObject={}
SomeObject.abc = "Some Value";
SOmeObject.myFunction = function(){
  // function body
}

In other file you can simply call SomeObject.abc & it will return string Some Value

You should use namespacing to avoid conflict of global window



来源:https://stackoverflow.com/questions/38126949/access-javascript-variable-included-in-html-header-from-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!