【问题标题】:How to Import External jquery files in angular 2 template? [duplicate]如何在 Angular 2 模板中导入外部 jquery 文件? [复制]
【发布时间】:2016-07-17 21:19:34
【问题描述】:

我有一个 Angular 2 应用程序,它在组件中有一个模板,如下所示

app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    templateUrl: 'templates/homepage.html',
    directives: []  
})
export class AppComponent {

}

我需要在模板 homepage.html 中包含外部 jquery 文件

我尝试在 homepage.html 中包含如下外部脚本文件似乎不起作用

<script src="/src/js/jquery.min.js"></script>
<script src="/src/js/jquery.scrollex.min.js"></script>
<script src="/src/js/jquery.scrolly.min.js"></script>
<script src="/src/js/skel.min.js"></script>
<script src="/src/js/util.js"></script>    
<script src="/src/js/main.js"></script>

boot.ts 文件如下所示

/// <reference path="../typings/browser.d.ts" />
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from "./app.component";

bootstrap(AppComponent);

index.html

<html>
<head>
    <base href="/">
    <title>Angular 2 Boilerplate</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <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/es6/dev/src/testing/shims_for_IE.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>
    <script src="node_modules/angular2/bundles/router.dev.js"></script>
    <script src="node_modules/angular2/bundles/http.js"></script>

<!-- 
    <link rel="stylesheet" href="src/css/app.css"> -->
</head>
<body>
<my-app>Loading...</my-app>

<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
    System.import('app/boot')
            .then(null, console.error.bind(console));
</script>
</body>
</html>    

我尝试在 index.html 本身中加载 .js 文件,但这不起作用。我对 Angular 2 很陌生。

【问题讨论】:

    标签: jquery angular angular2-template


    【解决方案1】:

    组件模板中的脚本标签刚刚被删除。

    一种解决方法是强制添加 thw script 标签或使用require(...) 加载它。

    另见
    - angular2: including thirdparty js scripts in component
    - https://github.com/angular/angular/issues/7720
    - How to Append <script></script> in javascript?

    【讨论】:

    • 加载多个脚本的更简洁的方法?我有 5 个脚本要加载。
    猜你喜欢
    • 2023-03-04
    • 2016-09-02
    • 2016-09-07
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多