我不知道如何包含js文件并在angular2中使用jquery

时间:2016-02-23 11:36:13

标签: javascript jquery angular

我想在angular2的index.html中使用js文件和jquery函数

所以,我输入了



<head>
  <script>document.write('<base href="' + document.location + '" />');</script>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>

  <script src="https://code.angularjs.org/2.0.0-beta.6/angular2-polyfills.js"></script>
  <script src="https://code.angularjs.org/tools/system.js"></script>
  <script src="https://code.angularjs.org/tools/typescript.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.6/Rx.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.6/router.dev.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.6/http.dev.js"></script>

  <script src="javascript file path"></script>
  
<script>
System.config({
    typescriptOptions: {emitDecoratorMetadata: true},
    packages: {'modules': {format: 'register', defaultExtension: 'js'}}
});

System.import('modules/app/main')
    .then(null, console.error.bind(console));
</script>
</head>
<body>
<my-app></my-app>
</body>
&#13;
&#13;
&#13;

进入index.html,我可以使用

确认导入的javascript文件

&#34;网络&#34; chrome调试工具。它有一个常规的流程

在导入.ts和.js文件等

之类的angular2文件之前导入javascript文件

但它没有运作。

我不知道为什么javascript文件无效。

请帮帮我。

1 个答案:

答案 0 :(得分:2)

您只需在index.html和底部的组件类中包含jQuery库(在export class xyz{}类型之外

declare var jQuery: any

然后你就可以使用jQuery('#something')....