简体   繁体   English

将外部文件 js 添加到 Angular 9 项目中

[英]Add external file js to an Angular 9 project

I tried to add script.js to angular.json and use it in one component.我尝试将 script.js 添加到 angular.json 并在一个组件中使用它。 That's not working.那是行不通的。 Those who suggest to add script tag to my html file thats not a good idea.那些建议将脚本标签添加到我的 html 文件的人不是一个好主意。 Can someone suggest another idea or what I missed to make my script work?有人可以提出另一个想法或我错过了什么来使我的脚本正常工作吗?

I add my js file called script.js on angular.json我在 angular.json 上添加了名为 script.js 的 js 文件

 "scripts": [
          "src/assets/js/script.js"
        ]

And I declare a variable in slider.ts我在 slider.ts 中声明了一个变量

  declare const owlCarousel: any;
  ngOnInit() {
    owlCarousel();
  }

script.js脚本.js

$('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    dots:false,
    nav:true,
    mouseDrag:false,
    autoplay:true,
    animateOut: 'slideOutUp',
    responsive:{
        0:{
            items:1
        },
        600:{
            items:1
        },
        1000:{
            items:1
        }
    }
    });
        

Try this尝试这个

First add function in your script.js首先在您的 script.js 中添加 function

export function owlCarousel() {
    $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    dots:false,
    nav:true,
    mouseDrag:false,
    autoplay:true,
    animateOut: 'slideOutUp',
    responsive:{
        0:{
            items:1
        },
        600:{
            items:1
        },
        1000:{
            items:1
        }
    }
    });
}

And in your component add this line to import function owlCarousel并在您的组件中添加此行以导入 function owlCarousel

 import {owlCarousel} from '../../assets/js/script.js' /plz add correct path 

Notice that slider.ts is not mandatory请注意,slider.ts 不是强制性的

Hope useful.希望有用。

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

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