PatrickJS-starter icon indicating copy to clipboard operation
PatrickJS-starter copied to clipboard

How to load a 3rd party js file

Open b27lu opened this issue 10 years ago • 22 comments

I'm going to import a 3rd party js file in one of my angualr2 component.

import '../../../../js/d3gauge.js';
export class MemMonComponent {
    createMemGauge() {
        new drawGauge(this.opt);  //drawGauge() is a function inside d3gauge.js
    }
}

And, in the custom-typings.d.ts file, I added declare var drawGauge: any; and run typings install.

But when I start the angular2 app, it still complains ReferenceError: drawGauge is not defined. could you point out whether there's something wrong? Thanks.

Update: after adding <script src="./js/d3gauge.js"></script> to `index.html, it works. But in this case, this js file will load for each component. How can I config webpack to load this js file only for certain components?

b27lu avatar May 07 '16 15:05 b27lu

In my blog, I use emojione as flow:

  1. https://github.com/doxiaodong/darlin-angular2/blob/develop/src/app/base/emojione/emojione.d.ts
  2. https://github.com/doxiaodong/darlin-angular2/blob/develop/src/app/base/marked/marked.component.ts

Good luck for you!

doxiaodong avatar May 30 '16 08:05 doxiaodong

I've got this question too - and I don't understand the given answer

reach4thelasers avatar Jul 09 '16 23:07 reach4thelasers

I'm having a similar issue, I've tried require and it loads the script as soon as I run the app. Is there a way to load it only for certain components.

thanks.

kat3su avatar Jul 25 '16 00:07 kat3su

any answer regarding this issue???

srkkhan avatar Sep 27 '16 12:09 srkkhan

+1

lhammond avatar Oct 10 '16 17:10 lhammond

+1

j-nord avatar Nov 20 '16 17:11 j-nord

+1

bigOconstant avatar Nov 28 '16 04:11 bigOconstant

It seems to be another wrong decision of the angular team, and not the first.

I had a library of components developed following the oficial angular documentation based on SystemJs. It used "import javascript_file.js" to load external javascript files and worked. Tried to port to new webpack version and it doesnt work at all. Also, the size of the app loaded by the browser went from 2.7 MB to 3.3.

The last news I have are the new version jumps from 2 to 4. Angular project it's a chaos that seems to have lost the north. It is bloated and over engineering. I'm going to change to React.

YagoLopez avatar Dec 21 '16 18:12 YagoLopez

it really seems that the angular2 team does not care about existing implementations using their framework. In my opinion Angular2 will be great one day but actually it is nearly unusable. Not at least because of the 3rd party libraries. I know, this is not their fault at all but they are changing their framework really often and they do not update the docs in time. Last week i did an update for several angular2 tools and nothing worked until i downgraded it again. SystemJS produces a lot of errors at my place while webpack seems to work as expected....mostly.

But to be fair: The angular2 developers clearly say that this is still a very early version and its still beta. I think when they release the first stable version those errors will vanish....hopefully.

messerbill avatar Jan 28 '17 22:01 messerbill

After a lot of investigation i managed to use webpack and systemjs at the same time. In my very personal opinion recompile each time with webpack during development time is clearly overkill. You should at least have the chance of using both and be able to switch without problems, but at first instance they are incompatible. Webpack sould be used for production.

@messerbill It seems the latest version of typescript enable to include external libraries easier not forcing you to declare types so strictly but then you have to manage the way of load them as webpack plugins. Also my version of webstorm throws errors with the latest typescript. Im trapped lol.

Typescript and Web components are good idea but so many moving parts and so much bloated code.... For example, using angular-cli you end having aaaaall testing frameworks duplicated as angular dependencies and angular-cli dependencies (jasmine, protractor, karma, selenium webdriver). May be npm is the culprit. It is needed more stability and clear guides. Supposedly Angular 2 have been ended beta phase and have been released as stable version.

You can take a look to the code at https://yagolopez.github.io/material-light/. It is a material desing library. May be you can find it useful. Greetings.

YagoLopez avatar Feb 02 '17 22:02 YagoLopez

I am using angular-cli, want to use external js library d3gauge.js in my component. How to use it ?

ghost avatar Jun 14 '17 18:06 ghost

@manzer001 for angular-cli you can add js file in config file see 'scripts' ; `{ "project": {

},
"apps": [
    {
        "root": "",
        "outDir": "",
        "assets": [
            "content",
            "i18n",
            "favicon.ico"
        ],
        "index": "index.html",
        "main": "app/app.main.ts",
        "polyfills": "app/polyfills.ts",
        "test": "",
        "tsconfig": "../../../tsconfig.json",
        "mobile": false,
        "styles": [
            "content/scss/vendor.scss",
            "content/scss/global.scss"
        ],
        "scripts": [
            "./vendor/jqwidgets-framework/scripts/jquery-1.11.1.min.js",
            "./vendor/jqwidgets-framework/jqwidgets/jqx-all.js"
        ]
    }
],
"addons": [],
"packages": [],
"e2e": {
    "protractor": {
        "config": "src/test/javascript/protractor.conf.js"
    }
},
"lint": [
    {
        "project": "../../../tsconfig.json"
    },
    {
        "project": "../../../tsconfig-aot.json"
    }
],
"test": {
    "karma": {
        "config": "src/test/javascript/karma.conf.js"
    }
},
"defaults": {
    "styleExt": "scss",
    "prefixInterfaces": false,
    "inline": {
        "style": true,
        "template": false
    },
    "spec": {
        "component": false,
        "directive": false,
        "pipe": false,
        "service": false
    }
}

} `

Codefans-fan avatar Jun 26 '17 05:06 Codefans-fan

A sugerence:

try to use

require ('../../../../js/d3gauge.js')

With or without ".js" extension, I dont remember exactly.

YagoLopez avatar Jun 26 '17 11:06 YagoLopez

I‘m doing it that way, hope that would help.

Hulva avatar Aug 02 '17 15:08 Hulva

sorry, this may sound super rude but I'm closing long standing issues and refactoring the codebase. If you run into this problem again feel free to open another issue

PatrickJS avatar Sep 02 '17 09:09 PatrickJS

I have issue that:

  • i put my .js file into assets folder.
  • i config at angular-cli.json

But when i routing from one component to another, the .js file did not load. However, i refreshed page (F5) and now the .js file is loaded.

Could someone can elucidate to me why? It so inconvenience for me.

tuannn0107 avatar Sep 12 '17 08:09 tuannn0107

You can Import js locally to ts file like this : import '../../../scripts/custom.js';

then declare method name in custom.js that you want to use like this 👍 declare var fAlert; then in a place like ngOnInit directly use this method

fAlert();

keivankashani avatar Oct 15 '17 08:10 keivankashani

@keivankashani As i remember you cant use declare in js .

AnatoliiTretiakov avatar Oct 17 '17 17:10 AnatoliiTretiakov

@keivankashani Thank you so much it helped me I have been trying for 2 days but it worked now thank you

KhanHamidkhan avatar Nov 10 '17 03:11 KhanHamidkhan

@keivankashani Your solution is good ... it works but the problem is that while we build the code and run then we get a ReferenceError. Also that refreshes the page twice while loading. If anyone has faced this issued then please send some solution. here is the error attached. error

aaron170 avatar Nov 14 '17 09:11 aaron170