为什么我无法在 Angular 应用程序中使用 JS 文件?

问题描述 投票:0回答:2

我正在尝试在角度应用程序中包含一些存储在文件中的

js
方法。不幸的是,我无法使浏览器看到
js
文件。
我已经查看了这篇SO帖子并按照说明进行操作,但是浏览器看不到文件 (
Sources
) 部分。



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"
            ]
          }
javascript node.js angular typescript ecmascript-6
2个回答
1
投票

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

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);
     }
}

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


0
投票

是的,我这样做了,但没有结果。我两天寻找解决方案。 我怎样才能做到这一点?有结果吗?

提前致谢

© www.soinside.com 2019 - 2024. All rights reserved.