Angular 6 does not load Jquery when browsing between views

3

I am implementing a template for a dashboard that has jQuery, chart js etc. as a dependency. Everything went well until the time to implement the routes because now when I browse through them I get the error Uncaught TypeError: Can not read property 'jquery' of undefined The funny thing is that if I refresh manually (F5) it works perfectly but when I fly to navigate the routes, the message reappears and I do not load the charts or the dynamic tables

I have tried defining the path of the Jquery in package.json but nothing

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "auto-vision": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/auto-vision",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.js",
              "./node_modules/datatables.net/js/jquery.dataTables.js",
              "./node_modules/popper.js/dist/umd/popper.min.js"

              
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "auto-vision:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "auto-vision:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "auto-vision:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.js",
              "./node_modules/datatables.net/js/jquery.dataTables.js",
              "./node_modules/popper.js/dist/umd/popper.min.js"

            ],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "auto-vision-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "auto-vision:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "auto-vision:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "auto-vision"
}
<!doctype html>
<html class="loading" lang="en" data-textdirection="ltr">
<head>
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/vendors.css">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
  <meta name="description" content="Sitio web donde monitorizar todos los datos a tiempo real de sus maquinas.">
  <meta name="keywords" content="administrador, autovision, monitorizacion">
  <meta name="Alvaro Ramirez Palazon" content="AutoVision">
  <title>Panel  
  </title>
  <link rel="apple-touch-icon" href="../../assets/app-assets/images/ico/apple-icon-120.png">
  <link rel="shortcut icon" type="image/x-icon" href="../../assets/app-assets/images/ico/favicon.ico">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Quicksand:300,400,500,700"
  rel="stylesheet">
  <link href="https://maxcdn.icons8.com/fonts/line-awesome/1.1/css/line-awesome.min.css"
  
  rel="stylesheet">


  <!-- BEGIN VENDOR CSS-->
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/vendors.css">
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/vendors/css/tables/datatable/datatables.min.css">
  <!-- END VENDOR CSS-->
  <!-- BEGIN MODERN CSS-->
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/app.css">
  <!-- END MODERN CSS-->
  <!-- BEGIN Page Level CSS-->
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/core/menu/menu-types/horizontal-menu.css">
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/core/colors/palette-gradient.css">
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/vendors/css/charts/jquery-jvectormap-2.0.3.css">

 

  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/fonts/simple-line-icons/style.css">
  <link rel="stylesheet" type="text/css" href="../../assets/app-assets/css/core/colors/palette-gradient.css">
  <!-- END Page Level CSS-->
  <!-- BEGIN Custom CSS-->
  <link rel="stylesheet" type="text/css" href="../../assets/css/style.css">
  <!-- END Custom CSS-->



</head>
<body class="horizontal-layout horizontal-menu 2-columns   menu-expanded" data-open="hover"
data-menu="horizontal-menu" data-col="2-columns">



<app-root></app-root>
     
<!-- BEGIN VENDOR JS-->
<!--<script src="../node_modules/jquery/dist/jquery.js" type="text/javascript"></script>-->

<script src="../../assets/app-assets/vendors/js/vendors.min.js" type="text/javascript"></script>
 <!-- BEGIN VENDOR JS-->
 <!-- BEGIN PAGE VENDOR JS-->

 


 <script type="text/javascript" src="../../assets/app-assets/vendors/js/ui/jquery.sticky.js"></script>
 <script type="text/javascript" src="../../assets/app-assets/vendors/js/charts/jquery.sparkline.min.js"></script>
 <script src="../../assets/app-assets/vendors/js/charts/chart.min.js" type="text/javascript"></script>
 <script src="../../assets/app-assets/vendors/js/charts/raphael-min.js" type="text/javascript"></script>

 <script type="text/javascript" src="../../assets/js/jquery.knob.min.js"></script>

 <script src="../../assets/app-assets/vendors/js/charts/jvector/jquery-jvectormap-2.0.3.min.js"
 type="text/javascript"></script>
 <script src="../../assets/app-assets/vendors/js/charts/jvector/jquery-jvectormap-world-mill.js"
 type="text/javascript"></script>
 <script src="../../assets/app-assets/data/jvector/visitor-data.js" type="text/javascript"></script>
 <script src="../../assets/app-assets/vendors/js/tables/datatable/datatables.min.js" type="text/javascript"></script>

 <link rel="stylesheet" type="text/css" href="../../assets/app-assets/vendors/css/cryptocoins/cryptocoins.css">


 <!-- END PAGE VENDOR JS-->
 <!-- BEGIN MODERN JS-->
 <script src="../../assets/app-assets/js/core/app-menu.js" type="text/javascript"></script>
 <script src="../../assets/app-assets/js/core/app.js" type="text/javascript"></script>
 <script src="../../assets/app-assets/js/scripts/customizer.js" type="text/javascript"></script>
 <!-- END MODERN JS-->
 <!-- BEGIN PAGE LEVEL JS-->
 <script type="text/javascript" src="../../assets/app-assets/js/scripts/ui/breadcrumbs-with-stats.js"></script>
 <script src="../../assets/app-assets/js/scripts/pages/dashboard-sales.js" type="text/javascript"></script>

  <script src="../../assets/app-assets/js/scripts/tables/datatables/datatable-basic.js"
  type="text/javascript"></script>

  


 <!-- END PAGE LEVEL JS-->
</body>
</html>
    
asked by Alvaro 07.10.2018 в 20:14
source

1 answer

0

Your index.html has an unusual aspect, but hey, I would start trying to include this library as a dependency to your project.

  • Install the dependency
  •   

    npm install jquery --save

  • Include your dependency's path in your ./angular-cli.json
  •   

    "scripts": ["../ node_modules / jquery / dist / jquery.min.js"]

  • Call jQuery on your component
  •   

    import * as $ from 'jquery';

    With this you should be able to use jQuery within your component without problems

        
    answered by 07.10.2018 в 22:22