简体   繁体   中英

Problem importing external jquery files from CDN

I have an issue trying to use external jquery files from CDN, to my understanding Jquery files ought to be included in the top most part from other custom jquery files.

I have done the same but I am getting an error which I cannot seem to get away with it no matter how I changed the arrangement of the files from the angular.json file.

Below are the pictures of my setup and error message.

Angular error, as a result of importing external custom jquery files from CDN [ [1] ]

This is my angular.json setup arranged to ensure that jquery is in the top most part

   "styles": [
              "src/styles.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css",              
              "src/assets/css/customcss.css"

            ],
            "scripts": [ 
            "./node_modules/jquery/dist/jquery.js",
            "./node_modules/jquery/dist/jquery.min.js",
            "./node_modules/jquery/dist/jquery.slim.min.js",
            "./node_modules/popper.js/dist/umd/popper.min.js",  
            "./node_modules/bootstrap/dist/js/bootstrap.min.js",
            "src/assets/js/custom.js"

               ]

This is my angular index.html page where I using the extenal custom jquery library

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title> My site </title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
   <!-- custom css -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css">
    </head>

<body>

<!-- jQuery Custom Scroller CDN -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js"></script>

 <app-root></app-root>
</body>
</html>

You can use this setting in angular.json

"scripts": [
            "node_modules/jquery/dist/jquery.js",
            "node_modules/bootstrap/dist/js/bootstrap.js",
            "node_modules/datatables/media/js/jquery.dataTables.js",

Don't add script tag to index.html file

In component TS file you can declare and use it

declare const $: any;

ngOnInit() {
   $('#example').DataTable();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM