Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular中访问js文件_Javascript_Angular_Typescript_Function_File - Fatal编程技术网

Javascript 如何在Angular中访问js文件

Javascript 如何在Angular中访问js文件,javascript,angular,typescript,function,file,Javascript,Angular,Typescript,Function,File,我有一个组件,我试图在其中定义一个函数,该函数读取以下js文件并检查其中是否包含某个字符串值。我怎么做?它的路径是'../../assets/beacons.js'(来自我的组件),它的名称是beacons.js const allBeacons = { "RIPE": [ "84.205.64.0/24", "84.205.65.0/24", "84.205.67.0/

我有一个组件,我试图在其中定义一个函数,该函数读取以下js文件并检查其中是否包含某个字符串值。我怎么做?它的路径是
'../../assets/beacons.js'
(来自我的组件),它的名称是
beacons.js

const allBeacons = {
    "RIPE": [
        "84.205.64.0/24",
        "84.205.65.0/24",
        "84.205.67.0/24",
        "84.205.68.0/24",
        "84.205.69.0/24",
        "84.205.70.0/24",
        "84.205.71.0/24",
        "84.205.74.0/24",
        "84.205.75.0/24",
        "84.205.76.0/24",
        "84.205.77.0/24",
        "84.205.78.0/24",
        "84.205.79.0/24",
        "84.205.73.0/24",
        "84.205.82.0/24",
        "93.175.149.0/24",
        "93.175.151.0/24",
        "93.175.153.0/24"].map(i => i.toLowerCase()),
    "rfd.rg.net": [
        "45.132.188.0/24",
        "45.132.189.0/24",
        "45.132.190.0/24",
        "45.132.191.0/24",
        "147.28.32.0/24",
        "147.28.33.0/24",
        "147.28.34.0/24",
        "147.28.35.0/24",
        "147.28.36.0/24",
        "147.28.37.0/24",
        "147.28.38.0/24",
        "147.28.39.0/24",
        "147.28.40.0/24",
        "147.28.41.0/24",
        "147.28.42.0/24",
        "147.28.43.0/24",
        "147.28.44.0/24",
        "147.28.45.0/24",
        "147.28.46.0/24",
        "147.28.47.0/24"].map(i => i.toLowerCase())
}

您需要将js文件包含在捆绑包中,方法是告诉angular:

  • 打开
    angular.json
    ,并将路径添加到
    “scripts”
    数组:
  • 在组件文件的顶部(在类声明之前),将变量声明为全局变量,这样typescript就不会抱怨它(名称必须与js文件中的名称匹配):
  • 现在,您可以将其用作应用程序中的全局变量:

  • 刚刚发布了一个类似的问题(有一个有效的答案):。这是我在谷歌搜索“在angular中使用js函数”时得到的第一个结果。它不起作用,console.log说:“所有信标都没有定义”。我已经按照你建议的步骤做了。我必须将
    declare const allBeacons:Beacons
    放在组件之前,否则我会得到
    类成员不能有'const'关键字
    。是的,我的错,我的意思是在组件的同一个文件中,但在类声明之前,我只是编辑了我的原始答案感谢它的工作,实际上我把路径写错了,所以是我的错
    "scripts": ["src/assets/beacons.js"]
    
      type Beacons = {/* Define type if you want */} | any
      declare const allBeacons: Beacons 
    
    ngOnInit() {
      console.log(allBeacons)
    }