GithubHelp home page GithubHelp logo

isabella232 / angular-lazy-import-jquery-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from w11k/angular-lazy-import-jquery-example

0.0 0.0 0.0 84 KB

Small Angular App with lazy import of jQuery and a jQuery plugin

TypeScript 73.27% JavaScript 16.11% CSS 1.13% HTML 9.50%

angular-lazy-import-jquery-example's Introduction

angular-lazy-import-jquery-example

Small example of an Angular App with lazy import of jQuery and a jQuery plugin

In a modern Angular App you don't want to include jQuery. But sometimes you have to. If you have to, it would be great to include jQuery only when it's really unavoidable.

In this small example we show how to import jQuery dynamically, so you don't have to include it in your bulk of javascript code loaded at app startup. We use EcmaScript's new function-like dynamic import to achieve that. TypeScript and AngularCLI (WebPack) already support this.

How to

  • change module from es2015 to esnext in src/tsconfig.app.json
  • add jQuery and a plugin to the project with npm install jquery @types/jquery jquery-lazyload
  • create a button and dynamically load a module in the click-handler via import('./loader')
  • in the loader-module first import jQuery with a regular import statement, then make jQuery globally available with (window as any).jQuery = jQuery;
  • after "exporting" jQuery dynamically load the plugin via import('jquery-lazyload').then(()) => { }) (or use async/await instead of then)

For a more versatile setup please take a look at the source code.

Development

This project was generated with Angular CLI version 1.5.5.

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the -prod flag for a production build.

angular-lazy-import-jquery-example's People

Contributors

angular-cli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.