Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.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_Node.js_Angular_Typescript_Ecmascript 6 - Fatal编程技术网

Javascript 为什么我不能在Angular应用程序中使用JS文件?

Javascript 为什么我不能在Angular应用程序中使用JS文件?,javascript,node.js,angular,typescript,ecmascript-6,Javascript,Node.js,Angular,Typescript,Ecmascript 6,我试图在angular应用程序中包含一些存储在文件中的js方法。不幸的是,我无法让浏览器看到js文件。我已经阅读了这篇文章并按照说明操作,但浏览器没有看到文件(源代码)部分 Js文件 function toggleModal(id) { var modal = document.getElementById(id); $("#" + id).modal('toggle'); } function setPag(tableId) {

我试图在angular应用程序中包含一些存储在文件中的
js
方法。不幸的是,我无法让浏览器看到
js
文件。
我已经阅读了这篇文章并按照说明操作,但浏览器没有看到文件(
源代码
)部分



Js文件

 function  toggleModal(id) {
        var modal = document.getElementById(id);
        $("#" + id).modal('toggle');
    }

    function setPag(tableId) {
        $(tableId).DataTable();
    }
我的文件位于:

Root/
  assets/
    js/
      scr.js
组件中的用法

declare function setPag(id:string):any;
declare function toggleModal(id:string):any;

@Component({
  selector: 'index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
     public toggle(id:string){
        toggleModal(id);
     }
     public setPage(id:string)
     {
        setPag(id);
     }
}
Angular.json
脚本部分

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/AnubisUI",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/assets"

            ],

            "scripts": [
              "./src/assets/js/scr.js",
              "node_modules/chart.js/dist/Chart.js",
              "node_modules/hammerjs/hammer.min.js"
            ]
          }

试试这个,看看是否适合你

import * as helper from 'assets/js/scr.js'

@Component({
  selector: 'index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
     public toggle(id:string){
        helper.toggleModal(id);
     }
     public setPage(id:string)
     {
        helper.setPag(id);
     }
}

旁注:每个静态文件都应添加到资产文件夹中,以便您可以导入角度代码

浏览器不会加载该文件!它不会出现在
源代码
部分。您需要将脚本放入资产文件夹中。我也将其放入资产中,但它仍然看不到。是否有任何错误?如果仍然不起作用,你能把你的代码上传到github让我看一下吗?现在它似乎增加了资产,只有资产使它起作用。你可以发布你的答案,我会接受它。