为什么我不能在 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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

为什么我不能在android studio的android resource strings.xml文件中使用'&'?

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

为什么我不能在共享服务器上使用node.js?

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

为什么我不能在我的 Rails 应用程序中使用 MySQL 'year' 数据类型?

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    为什么我不能在android studio的android resource strings.xml文件中使用'&'?

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

    为什么我不能在共享服务器上使用node.js?

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

    为什么我不能在我的 Rails 应用程序中使用 MySQL 'year' 数据类型?

热门标签

归档