How to Use External Javascript in TypeScript Angular - Stack Overflow

I want to use jquery and easypiechart js file's functions in typescript.It doesn't work this w


I want to use jquery and easypiechart js file's functions in typescript.

It doesn't work this way.

How to define these script what i specified in code as typescript ?


indexponent.ts

import { Component, OnInit } from '@angular/core';

import * as $ from "../../../../../assets/plugins/jquery/jquery.min.js";
import { easyPieChart } from "../../../../../assets/plugins/easypiechart/jquery.easypiechart.min.js";

// these above 2 js files are defined in angular.json script section

@Component({
  selector: 'app-index',
  templateUrl: './indexponent.html',
  styleUrls: ['./indexponent.scss']
})

export class IndexComponent implements OnInit {

  constructor() {}

  ngOnInit() {

    //$(function(){
    //  $('.easypiechart').easyPieChart();
    //});

    // How to write this above script as typescript ?????????????????????

  }
}

I want to use jquery and easypiechart js file's functions in typescript.

It doesn't work this way.

How to define these script what i specified in code as typescript ?


index.ponent.ts

import { Component, OnInit } from '@angular/core';

import * as $ from "../../../../../assets/plugins/jquery/jquery.min.js";
import { easyPieChart } from "../../../../../assets/plugins/easypiechart/jquery.easypiechart.min.js";

// these above 2 js files are defined in angular.json script section

@Component({
  selector: 'app-index',
  templateUrl: './index.ponent.html',
  styleUrls: ['./index.ponent.scss']
})

export class IndexComponent implements OnInit {

  constructor() {}

  ngOnInit() {

    //$(function(){
    //  $('.easypiechart').easyPieChart();
    //});

    // How to write this above script as typescript ?????????????????????

  }
}
Share asked Feb 5, 2019 at 3:52 canmustucanmustu 2,6694 gold badges25 silver badges40 bronze badges 4
  • 1 This looks dublicate of this. stackoverflow./questions/47346559/… – Bijay Yadav Commented Feb 5, 2019 at 3:56
  • as pointed out it`s a duplicate also why would you use jquery in angular? any specific reason for that? – Manit Commented Feb 5, 2019 at 4:08
  • Don't use jquery in angular. Choose one framework or the other – Our_Benefactors Commented Feb 5, 2019 at 4:09
  • Possible duplicate of using external JS libraries in my angular 2 project – adiga Commented Feb 5, 2019 at 4:39
Add a ment  | 

4 Answers 4

Reset to default 2

From the above question,it looks like jquery.easypiechart.min.js is the one that you need to use in your angular application as external js.

  1. Put the js under assets folder say /assets/js/jquery.easypiechart.min.js
  2. Goto your projects angular.json file and under scripts node of architect node put as an entry in the array.

    "scripts": [ "./node_modules/jquery/dist/jquery.min.js",
    "./src/assets/js/jquery.easypiechart.min.js" ]

Now you can refer the external js in any of your projects ponents

  declare var $: any;// referencing jQuery library
    @Component({
      selector: 'app-index',
      templateUrl: './index.ponent.html',
      styleUrls: ['./index.ponent.scss']
    })

    export class IndexComponent implements OnInit {
      constructor() {}
      ngOnInit() {
      $(document).ready(function () {
          //accessing easypiechart.min.js.
          $('.easypiechart').easyPieChart();
         });
      }
    }

If you have included them in the scripts or index.html, you don't have to import them to the .TS file again

Use declare instead and it should work What does 'declare' do in 'export declare class Actions'?

Instead of putting it in asset folder you should use it as node_modules dependency

For easy pie chart run this npm i easy-pie-chart --save & for jquery run npm i jquery

Normally you don't want to use jquery in Angular, because it usually implies to modify directly the DOM, which is a bad practice, but there is the way to do it: https://medium./all-is-web/angular-5-using-jquery-plugins-5edf4e642969

If you wanna plot a pie chart or other types of charts, you could use ng2-charts instead, it will allow you to use charts.js with Angular and Typescript.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744862210a4597771.html

相关推荐

  • How to Use External Javascript in TypeScript Angular - Stack Overflow

    I want to use jquery and easypiechart js file's functions in typescript.It doesn't work this w

    1天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信