简体   繁体   English

Angular-cli bootstrap v3和jquery? Angular 2.0

[英]Angular-cli bootstrap v3 and jquery? Angular 2.0

So I am about to start a project and I think Angular 2.0 seems a very solid framework to work with: Routing, components, directives, etc. However, there are many stuff already built using bootstrap v3 and jQuery that I would like to use. 所以我即将开始一个项目,我认为Angular 2.0似乎是一个非常可靠的框架:路由,组件,指令等。但是,有很多东西已经使用我想使用的bootstrap v3和jQuery构建。 I was wondering if it is possible to integrate bootstrap v3 and JQuery using the structure made by Angular-cli? 我想知道是否可以使用Angular-cli制作的结构集成bootstrap v3和JQuery?

By the way I've seen other Angular 2 seeds using Gulp, but they always recommend the official Angular-cli instead. 顺便说一句,我已经看过其他使用Gulp的Angular 2种子,但他们总是推荐官方的Angular-cli。

Also I am new on how brining an existing JS library into Typescript actually works and if jquery's $ global will interfere with typescript. 另外,我对如何将现有的JS库打包成Typescript实际工作以及jquery的$ global是否会干扰打字稿也是新的。

Note: I am aware of ng2-bootstrap, but I don't think that everything is compatible with this approach since it doesn't use jquery as dependency. 注意:我知道ng2-bootstrap,但我不认为一切都与这种方法兼容,因为它不使用jquery作为依赖。 I am also aware that using jquery in angular is not the best practice, but I think there are some cases in which you really need it for plugins or dependencies. 我也知道在角度中使用jquery不是最好的做法,但我认为在某些情况下你真的需要它来插件或依赖。

I can suggest you a workaround until they get better support for 3rd party libs. 我可以建议你一个解决方法,直到他们得到更好的支持第三方库。 It worked for me :) 它对我有用:)

After typing 打字后

npm install jquery 

and

 typings install jquery --ambient --save

In your angular-cli-build.json , make sure it remains like this way 在你的angular-cli-build.json中,确保它保持这种方式

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'jquery/**/*.js'
    ]
  });
};

and in your system-config.ts: 并在您的system-config.ts中:

/** Map relative paths to URLs. */
 const map: any = {
   'jquery': 'vendor/jquery'
 };

in your src/index.html add this line 在你的src / index.html中添加这一行

<script src="/vendor/jquery/dist/jquery.min.js" type="text/javascript"></script>

now in your component where you want to use jquery , write this way 现在在你想要使用jquery的组件中,这样写

declare var $:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
    $.("button").click(function(){
       // now you can DO, what ever you want 
     });
     console.log();
  }
}

I am doing it this way, in my project. 我这样做,在我的项目中。 Hope It might help you :) . 希望它可能会帮助你:)。

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

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