angular2怎么使用第三方的庫(jquery等)


網上找了很多教材都搜索不到該部分類型,自己測試了下寫了該教程。

場景說明:項目需要使用bootstrap,眾所周知bootstrap沒有時間日期控件的,需要使用第三方控件,我對如何在angular2中使用第三方控件比較恐慌,我項目使用angular-cli構建的。

解決流程
1:配置package.json添加新的依賴,然后進行update,下載新的庫
   
   
   
           
  1. "jquery":"*",
  2. "tether":"*",
  3. "bootstrap":"*",
  4. "moment":"*",
  5. "eonasdan-bootstrap-datetimepicker":"*"
2:  配置angular-cli.json
   
   
   
           
  1. "styles": [
  2. "../node_modules/bootstrap/dist/css/bootstrap.min.css",
  3. "../node_modules/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css",
  4. "styles.css"
  5. ],
  6. "scripts": [
  7. "../node_modules/jquery/dist/jquery.min.js",
  8. "../node_modules/tether/dist/js/tether.min.js",
  9. "../node_modules/bootstrap/dist/js/bootstrap.min.js",
  10. "../node_modules/moment/min/moment.min.js",
  11. "../node_modules/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"
  12. ],
3: 在模版中使用datatimepicker插件
   
   
   
           
  1. <div class="container">
  2. <div class="row">
  3. <div class='col-sm-6'>
  4. <div class="form-group">
  5. <div class='input-group date' id='datetimepicker1'>
  6. <input type='text' class="form-control" />
  7. <span class="input-group-addon">
  8. <span class="glyphicon glyphicon-calendar"></span>
  9. </span>
  10. </div>
  11. </div>
  12. </div>
  13. </div>
  14. </div>
4:在組件中對該組件進行實例化
   
   
   
           
  1. declare var $:any;
  2. @Component({
  3. selector:"app-root",
  4. templateUrl:"bootstrap.template.html"
  5. })
  6. export class BootstrapComponent extends OnInit{
  7. ngOnInit(): void {
  8. $(function () {
  9. $('#datetimepicker1').datetimepicker();
  10. });
  11. }
  12. }
這里注意需要聲明$變量,如果你使用jquery那么聲明jquery變量,如果不聲明,console會有提示錯誤,這里的聲明為什么會起作用我具體也不是很清楚,也許類似d.ts的作用吧。






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM