How to import jQuery to Angular2 TypeScript projects?

前端 未结 7 1853
一向
一向 2020-12-01 14:48

I want to wrap some jQuery code in an Angular2 directive.

I installed jQuery library for Typings into my project with the following command:

typings in

相关标签:
7条回答
  • 2020-12-01 15:23

    You should have a typings.json that points to your jquery typing file. Then:

    systemjs.config (notice map setting for jquery)

    System.config({
        defaultJSExtensions: true,
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        map: {
            'app':  'app',
            jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js',
            material: 'npm:material-design-lite/dist/material.min.js',
    
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            ....
        },
        packages: {
            app: { main: 'main', format: 'register', defaultExtension: 'js' },
            'rxjs': { defaultExtension: 'js' }
        },
    });
    

    In component:

    import $ from 'jquery';
    

    Then use $ as usual.

    0 讨论(0)
提交回复
热议问题