简体   繁体   English

从js文件访问变量到ts

[英]accessing variables from js files to ts

Hi I'm trying to update an existing angular app from version 2 to 9, I'm starting by updating it to angular 4 then continuing later, I was able to update the packages that require updating, but now I'm getting an error that a variable is not defined here is the app structure嗨,我正在尝试将现有的 angular 应用程序从版本 2 更新到 9,我首先将其更新到 angular 4,然后继续稍后,我能够更新需要更新的软件包,但现在我收到错误此处未定义变量是应用程序结构

I have legend.component.js我有legend.component.js

var LegendSingleton=function(() {
               ...
               createInstance
               ...
               getInstance
               ...

main.component.js

var myExtObject = {
               ...
               legend : LegendSingleton.getInstance()
               ...

then in layoutindicateur.component.ts然后在layoutindicateur.component.ts

              ...
var myExtObject = require('../../../assets/geoFunction/main.component.js');
some use of myExtObject 
              ...

also in angular-cli.json i declared those js files in scripts :同样在angular-cli.json我在scripts中声明了这些 js 文件:

...
"assets/geoFunction/component/legend.component.js",
...
"assets/geoFunction/main.component.js"

I'm getting error Uncaught (in promise): ReferenceError: LegendSingleton is not defined I tried to import the legend js file in main js file using var LegendSingleton = require('../geoFunction/component/legend.component.js');我收到错误Uncaught (in promise): ReferenceError: LegendSingleton is not defined我尝试使用var LegendSingleton = require('../geoFunction/component/legend.component.js');在主 js 文件中导入图例 js 文件var LegendSingleton = require('../geoFunction/component/legend.component.js'); but got error of getInstance not a functiongetInstance的错误不是 function

You normally just need to add the files to your angular-cli.json files, in the scripts section.您通常只需将文件添加到scripts部分中的angular-cli.json文件中。

Then, in your layoutindicateur.component.ts file, declare myExtObject below the angular imports然后,在您的layoutindicateur.component.ts文件中,在 angular 导入下声明myExtObject

//import ....
declare let myExtObject: any;

Then use it in your component file然后在你的组件文件中使用它

myExtObject.legend.legendFunction()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM