1

I am having some jquery script in "/src/assets/js/auth.js".

I have included the file and jquery plugin in angular.cli.json file.

angular.cli.json:

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

Finally I have imported jquery in the app.component.ts.

app.component.ts:

import * as $ from 'jquery';

To make sure whether jquery works or not, I have put the jquery code in ngOnInit() {} function and its works well.

But when I am placing it as an external js file, I will always return the error: Uncaught Reference error $ is not defined

Please someone help me to fix this issue.

1

1 Answer 1

3

You need to include jquery file before the plugin

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

And replace

import * as $ from 'jquery';

with

declare let $: any;

Otherwise, jquery will work but $ will not contain the plugin's function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.