角轮播js一段时间加载一些时间不

时间:2018-12-31 06:42:11

标签: javascript angular

我创建了一个script.service.ts服务,以在页面加载后添加js。 但是,它有时会起作用,有时却不会。

我已将所有代码附加到script.service.ts文件中。请查看并帮助我。

home.component.html->这是显示轮播。

<ul class="jobcatgoryslide">
    <div class="carousel js-flickity" data-flickity-options='{ "cellAlign": "right" , "contain": "true", "pageDots": "false" , "autoPlay": 1500 }'>
      <li class="carousel-cell" *ngFor="let competency of competenciesList?.competencyList">
        <div class="categoryBlock">
          <div class="cateIcon">
            <img [src]="competency.image | image:competenciesList.s3_img_url" height="64">
          </div>
          <h3>{{ competency.title_en }}</h3>
          <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque</p>

        </div>
      </li>
    </div>
  </ul>

home.component.ts->这是使用脚本服务。

this.script.load('flickity').then(data => {
      console.log(data);
    }).catch(error => console.log(error));

script.service.ts->它是脚本服务文件。

import {Injectable} from "@angular/core";
import {ScriptStore} from "./script.store";

declare var document: any;

@Injectable()
export class Script {

private scripts: any = {};

constructor() {
    ScriptStore.forEach((script: any) => {
        this.scripts[script.name] = {
            loaded: false,
            src: script.src
        };
    });
}

load(...scripts: string[]) {
    var promises: any[] = [];
    scripts.forEach((script) => promises.push(this.loadScript(script)));
    return Promise.all(promises);
}

loadScript(name: string) {
    return new Promise((resolve, reject) => {
        //resolve if already loaded
        if (this.scripts[name].loaded) {
            resolve({script: name, loaded: true, status: 'Already Loaded'});
        }
        else {
            //load script
            let script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = this.scripts[name].src;
            if (script.readyState) {  //IE
                script.onreadystatechange = () => {
                    if (script.readyState === "loaded" || script.readyState === "complete") {
                        script.onreadystatechange = null;
                        this.scripts[name].loaded = true;
                        resolve({script: name, loaded: true, status: 'Loaded'});
                    }
                };
            } else {  //Others
                script.onload = () => {
                    this.scripts[name].loaded = true;
                    resolve({script: name, loaded: true, status: 'Loaded'});
                };
            }
            script.onerror = (error: any) => resolve({script: name, loaded: false, status: 'Loaded'});
            document.getElementsByTagName('head')[0].appendChild(script);
        }
    });
}

}

script.store.ts->此处脚本存储ts文件

interface Scripts {
    name: string;
    src: string;
}
export const ScriptStore: Scripts[] = [
    { name: 'flickity', src: '/assets/js/slider.js' }
];

请帮助我。谢谢你。

1 个答案:

答案 0 :(得分:1)

我已经安装了npm i ngx-flickity

FlickityModule导入应用程序的模块中:

home.module.ts

import { FlickityModule } from 'ngx-flickity';

@NgModule({
  imports: [
    FlickityModule
  ]
})

home.component.html

<div class="carousel js-flickity" [flickity]="{cellAlign: 'right', contain: true,
      pageDots: false , autoPlay : 1500}">
      <li class="carousel-cell" *ngFor="let competency of competenciesList?.competencyList">
        <div class="categoryBlock">
          <div class="cateIcon">
            <img [src]="competency.image | image:competenciesList.s3_img_url" height="64">
          </div>
          <h3>{{ competency.title_en }}</h3>
          <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque</p>

        </div>
      </li>
    </div>

它工作正常。