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

贝尔科维奇·阿德里安

我试图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
-Scripts 部分

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在单独的html和js文件中使用全局变量?

为什么我不能在我的 Angular 应用程序中使用 MomentJS 向日期添加年份?

我的Angular JS应用程序可在桌面浏览器中使用,但不能在移动浏览器中使用

在angular cli应用程序中使用vtk.js / glsl文件

为什么我不能在我的 Angular 应用程序中将此用户对象设置为 null?

为什么我不能在 Entity Framework Core Sqlite 文件中使用文件扩展名?

为什么我不能在这个 Angular 9 应用程序中将变量从组件传递到服务?

为什么我不能在apache中阻止单个node.js文件?

为什么我不能在 R 中使用 left_join 合并两个文件?

为什么我不能在Android Studio中使用ImageButton上传图像文件?

为什么我不能在android 10中使用白色文件?

为什么我不能在xampp中使用php编写文件?

Laravel 5:为什么我不能在语言文件中使用整数和浮点数?

为什么我不能在文件夹中使用chmod 000?

为什么我不能在aspx.cs文件中使用c#类?

为什么我不能在Windows 7上编辑“程序文件”文件?

为什么我不能在我的基本 React 应用程序中使用 promise?

为什么我的正则表达式不能在JS中使用?

在我的Angular JS应用程序中,我想知道控制器中使用了哪些服务,模块?

为什么我不能在 swift 3 的应用程序中使用后台模式?

为什么我不能在权限为 000 的文件上使用 `$ sudo echo "hello" > file`?

为什么我无法在Wpf中使用应用程序内的WebView来导航html文件?

为什么我不能使用默认文件(如记事本)运行应用程序?

rails 应用程序中使用的 .json 文件是什么?

我想在我的Angular Web应用程序中使用Azure应用程序服务的``应用程序设置''变量

在angular js的单独文件中定义应用程序

为什么我们不能在路径中使用@来“以管理员身份运行”批处理文件?

为什么我不能在类/静态方法中使用私有的、内部的、文件私有的方法?

为什么我不能在Windows Server 2008中使用system()从服务启动批处理文件?