简体   繁体   English

将 AOS 库与 Angular4 应用程序集成

[英]Integrate AOS library with Angular4 application

I am working on Angular 4.4.6 application and I want to implement some animations on scroll using the awesome plugin AOS我正在开发 Angular 4.4.6 应用程序,我想使用很棒的插件AOS在滚动时实现一些动画

my code:我的代码:

app.component.ts app.component.ts

import * as AOS from 'aos';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.scss'],
 encapsulation:ViewEncapsulation.None
})
export class AppComponent implements OnInit {
 ngOnInit(){
  AOS.init();
 }
}

app.component.html应用程序组件.html

<div class="box" data-aos="slide-left">
      <img src="assets/imgs/seo.png" alt="">
</div>

angulr.cli.json angulr.cli.json

"styles": [
    "../node_modules/aos/dist/aos.css",
    "../node_modules/animate.css/animate.min.css",
    "styles.scss"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/aos/dist/aos.js"
  ],

I've tried the answer here with no hope https://stackoverflow.com/a/44808496/4183947我在这里尝试了没有希望的答案https://stackoverflow.com/a/44808496/4183947

Note: I've no errors in my console.注意:我的控制台中没有错误。

I'm trying to get the same thing working for a site I'm migrating to Angular 4.2.4.我正在尝试为我正在迁移到 Angular 4.2.4 的网站做同样的事情。 Since you included the path to the AOS stylesheet in your angular.cli.json you probably already installed AOS via:由于您在angular.cli.json包含了 AOS 样式表的路径,因此您可能已经通过以下方式安装了 AOS:

npm install aos --save

I got it running with the following:我让它运行如下:

// angular.cli.json
...
"styles": [
  "../node_modules/aos/dist/aos.css",
  "styles.scss"
],
"scripts": [],
...

and:和:

// app.component.ts
import { Component, OnInit } from '@angular/core';

import * as AOS from 'aos';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'app';

  ngOnInit() {
    AOS.init();
  }
}

and in my case I have markup in a HomeComponent:就我而言,我在 HomeComponent 中有标记:

// home.component.html
...
<div data-aos="fade-up" data-aos-once="true">
  the content
</div>
...

I might explore using AOS via the Angular Dependency Injection system in the future, as described in animate into view when scrolled to angular2 but simply importing AOS in app.component.ts is working so far.将来我可能会通过 Angular Dependency Injection 系统探索使用 AOS,如滚动到 angular2 时动画进入视图中所述,但到目前为止,只需在app.component.ts导入 AOS app.component.ts

npm install aos --save

Edit angular.cli.json编辑 angular.cli.json

// angular.cli.json
...
"styles": [
"../node_modules/aos/dist/aos.css",
"styles.scss"
],
"scripts": [
"../node_modules/aos/dist/aos.js"
],
...

App Component应用组件

// app.component.ts
import { Component, OnInit } from '@angular/core';

import * as AOS from 'aos';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'app';

  ngOnInit() {
    AOS.init();
  }
}

In component html or where you want to add animation在组件 html 或要添加动画的位置

...
<div data-aos="fade-up" data-aos-once="true">
  the content
</div>
...

Terminal终端

# if you use npm
npm install aos -s

# if you use yarn
yarn add aos

styles.scss样式.scss

@import 'aos/dist/aos.css'; //<------ Add this line to the top

app.component.ts app.component.ts

import { Component } from '@angular/core';
import * as AOS from 'aos'; //<------ Add this line

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent {
    constructor() {
        AOS.init(); //<------ Add this line
    }
}

For angular9+对于 angular9+

do

npm install aos --save

add js and css file in angular.jsonangular.json添加 js 和 css 文件

            "styles": [
              "./node_modules/aos/dist/aos.css"
            ],
            "scripts": [
              "./node_modules/aos/dist/aos.js"
            ]

In the app component (your root component), call AOS.init()在 app 组件(您的根组件)中,调用 AOS.init()

export class AppComponent {
  title = 'my-app';

  ngAfterViewInit() {
    AOS.init();
  }
}

Stop your if you are already running and then start again because you have changes in angular.json file如果您已经在运行,请停止运行,然后重新开始,因为您在 angular.json 文件中有更改

Make sure you have install all 3 Dependencies.确保您已安装所有 3 个依赖项。

npm i classlist-polyfill
npm i lodash.debounce
npm i lodash.throttle

you can check here: https://www.npmjs.com/package/aos#init-aos你可以在这里查看: https : //www.npmjs.com/package/aos#init-aos

运行这个命令

npm i --save-dev @types/aos

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

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