如何在angular2(typescript)上使用带jquery的js库?

时间:2016-07-24 18:29:50

标签: jquery angular scrollmonitorjs

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

我无法理解如何使用js库。 我有主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>

general main.ts

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

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

bootstrap(AppComponent);

并清空app.component。

1 个答案:

答案 0 :(得分:1)

为了使用jQuery,首先你必须在index.html中导入, 您可以在组件的构造函数中初始化代码

constructor(){
   .....
   $('#home').height($(window).height());
}

如果IDE显示错误,您可以在类顶部声明任何类型的$,如下所示: -

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

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

看也可以帮到你