typescript - Angular 4.x: Hvordan laste inn chance.js i ngOnInit()?


Jeg er ny i Angular 4.x og jeg vil laste change.js i ngOnInit()-funksjonen. Hvordan kan jeg gjøre dette? Jeg har prøvd noen ting, men ingen fungerte:

her er koden min :

import { Component, OnInit, Input } from '@angular/core';

...
.....
    declare var chance: any;

    import 'src/assets/js/chance.js';
    ....
    ....
    export class ComposantComponent implements OnInit {
    ....
    ....
          ngOnInit() {
            this.http.get("src/app/datas/data.json")
              .subscribe((data)=> {
                setTimeout(()=> {
                  this.data = data.json();
                  this.data.forEach(function(row, index) {
                    row.checked = false;
                     // use chance here ... 
                  });

                  console.log(this.data[0]);
                  // console.log(chance);

                }, 500);
              });
          }
    ....
    ....
    ....
    }

Kan du være så snill å hjelpe?

Svar #1

endelig funnet en løsning.

I index.html

  <script src="http://chancejs.com/chance.min.js"></script>

i min component.ts

import { Component } from '@angular/core';

declare var Chance: any; // for externals librairies

I klassen min:

  chance: any;
  constructor(){
    this.chance  = new Chance();
    console.log(this.chance.ip());
  }

føl deg fri til å forbedre denne løsningen ...

Relaterte artikler