如何在 angular2(打字稿)上使用带有 jquery 的 js 库?

How use js library with jquery on angular2 (typescript)?

我尝试使用 typescript 将 angular js 迁移到 angularjs2。

我不明白如何使用 js 库。 我有 main app.component ,我想在其中初始化一些库,例如简单的行:

$('#home').height($(window).height());

并通过attribute data-spy="scroll" data-target="#header-navbar" data-offset="51"

初始化

Index.html

<body data-spy="scroll" data-target="#header-navbar" data-offset="51" class="pace-done">

    <pm-app>Loading App...</pm-app>


    <!-- ================== BEGIN BASE JS ================== -->
    <script src="assets/plugins/jquery/jquery-1.9.1.min.js"></script>
    <script src="assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
    <script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
    <!--[if lt IE 9]>
        <script src="assets/crossbrowserjs/html5shiv.js"></script>
        <script src="assets/crossbrowserjs/respond.min.js"></script>
        <script src="assets/crossbrowserjs/excanvas.min.js"></script>
    <![endif]-->
    <script src="assets/plugins/jquery-cookie/jquery.cookie.js"></script>
    <script src="assets/plugins/scrollMonitor/scrollMonitor.js"></script>
<script src="assets/js/landing-apps.js"></script>

    <!-- ================== BEGIN BASE JS ================== -->
      <script src="assets/plugins/pace/pace.min.js"></script>
    <!-- ================== END BASE JS ================== -->
<!-- ================== END BASE JS ================== -->
    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>

    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>


    <!-- Required for http -->
    <script src="node_modules/angular2/bundles/http.dev.js"></script>

    <!-- Required for routing -->
    <script src="node_modules/angular2/bundles/router.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

</body>

一般main.ts

import { bootstrap }    from 'angular2/platform/browser';

// Our main component
import { AppComponent } from './app.component';

bootstrap(AppComponent);

空 app.component.

为了使用 jQuery,您首先必须在 index.html 中导入, 比你可以在组件

的构造函数中初始化你的代码
constructor(){
   .....
   $('#home').height($(window).height());
}

万一 IDE 显示错误,您可以像这样在 class 顶部声明任何类型的 $ :-

...some  code here 
declare var $ : any;

export class App{
..///code goes here
}

另请参阅可能对您有所帮助