简体   繁体   English

在Angular 2组件上包含Javascript文件(jQuery,Bootstrap等)

[英]Include Javascript files (jQuery, Bootstrap, etc…) on a Angular 2 Component

I'm creating a SPA that have two parts. 我正在创建一个包含两个部分的SPA。 The landing page that is public and it's used to explain things about the project and to show up a login form. 公开的登录页面,用于解释有关该项目的事情并显示登录表单。 The other part of the application is not public and only registered users can access. 该应用程序的另一部分不是公开的,只有注册用户可以访问。

Each part of the site (Home & Dashboard) have different components. 站点的每个部分(“主页”和“仪表板”)都有不同的组件。 The problem I'm facing is that these component have different Javascript files that I already have bundled (home.js & dashboard.js). 我面临的问题是这些组件具有我已经捆绑的不同Javascript文件(home.js和dashboard.js)。 I've found in Internet different solutions but they do not look right to time. 我在Internet上发现了不同的解决方案,但是它们看起来不合适。

Some details about the environment is that the application was scaffolded by Angular CLI so webpack is included, in case it makes easier the task. 有关环境的一些详细信息是该应用程序由Angular CLI进行了脚手架安装,因此包含了webpack,以防使任务变得更容易。

I would like to know if there is a proper way to do it that meets Angular 2 Good practices. 我想知道是否有符合Angular 2良好做法的正确方法。

Any help would be appreciated. 任何帮助,将不胜感激。

-Inside angular-cli.json there is a section called apps. -在angular-cli.json内部有一个名为apps的部分。 Inside that there is scripts section you can import your js files there angular cli will bundle that js files into scripts.bundle.js and put it to index.html 在里面有脚本部分,您可以导入您的js文件,在那里cli cli将这些js文件捆绑到scripts.bundle.js中并将其放入index.html

"apps": [
{
  "scripts": ["../node_modules/jquery/dist/jquery.min.js",
  "../node_modules/hammerjs/hammer.js",
  "path_to_your_file/dashboard.js",
  "path_to_your_file/home.js"]
}

-In another way you can import them inside index.html in classic way. -以另一种方式,您可以采用经典方式将其导入index.html中。

I'm not sure but i imported 'hammerjs' inside app.module.ts but i think it works because it is a module which installed via npm. 我不确定,但我在app.module.ts中导入了“ hammerjs”,但我认为它可以工作,因为它是通过npm安装的模块。

import { MaterialModule } from '@angular/material';
import 'hammerjs';

like this. 像这样。 there is nothing more down about hammerjs. 没有更多关于hammerjs的信息了。 You can turn your js files to module and install them like hammerjs. 您可以将js文件转换为模块,然后像Hammerjs一样安装它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM