Angular2 How to use third-party libraries (jquery, etc.)

Source: Internet
Author: User

On the internet to find a lot of textbooks are not search for this part of the type, self-test to write the tutorial.
Scenario Description: The project needs to use bootstrap, it is well known that Bootstrap has no time-date control, need to use a third-party control, I am afraid of how to use third-party controls in Angular2, my project uses ANGULAR-CLI build.
Resolution 1: Configure Package.json to add a new dependency, then update to download the new library
  
 
  1. "jquery":"*",
  2. "tether":"*",
  3. "bootstrap":"*",
  4. "moment":"*",
  5. "eonasdan-bootstrap-datetimepicker":"*"
2: Configure 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: Using the Datatimepicker plugin in the stencil
  
 
  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: Instantiating the component in an assembly
  
 
  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. }
Note here that you need to declare the $ variable, if you use jquery then declare the jquery variable, if you do not declare, console will have a hint error, the statement here why it works I am not very clear, perhaps similar to the role of D.ts.


From for notes (Wiz)

Angular2 How to use third-party libraries (jquery, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.