简体   繁体   English

Initialise AOS - Angular Universal Server Side Rendering 中滚动库上的动画

[英]Initialise AOS - Animate on scroll library in Angular Universal Server Side Rendering

I've implemented AOS - Animate on scroll library into my project to deal with the animations.我已经在我的项目中实现了 AOS - Animate on scroll library 来处理动画。 But as I convert my Angular project into Angular universal.但是当我将我的 Angular 项目转换为 Angular 通用时。 Error start popping.错误开始弹出。

ERROR ReferenceError: document is not defined
    at n (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:16908:14433)
    at Object._ [as init] (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:16908:1386)
    at AppComponent.ngOnInit (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:106:42)
    at callHook (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:149051:22)
    at callHooks (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:149020:17)
    at executeInitAndCheckHooks (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:148971:9)
    at refreshView (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:156008:21)
    at renderComponentOrTemplate (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:156107:9)
    at tickRootContext (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:157338:9)
    at detectChangesInRootView (/Users/syahiruddin/RVI/app/dist/functions/server/main.js:157363:5)

I could not locate the document that is not defined by the error.我找不到错误未定义的文档。

Please if someone could shine the light to it.请如果有人可以照亮它。

Here is how I implemented AOS into the component:以下是我在组件中实现 AOS 的方式:

import { Component, OnInit } from '@angular/core';
import { SeoService } from './core/services/seo.service';
// 🚨 TODO: FIX AOS for angular universal
import * as AOS from 'aos';
@Component({
  selector: 'rvi-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  constructor(private seo: SeoService) {}

  ngOnInit() {
    this.seo.initDefaultMetaTags();
    // ERROR ReferenceError: document is not defined
    // 🚨 TODO: fix this!!
    AOS.init({ once: true, duration: 1000 });
  }
}

I'm not sure if there is a caveat and just a workaround, but I'm able to bypass the error by wrapping it with Angular "isPlatformBrowser" from @angular/common.我不确定是否有警告和解决方法,但我可以通过使用来自@angular/common 的 Angular "isPlatformBrowser" 包装它来绕过错误。

Here is the solution:这是解决方案:

1 import PLATFORM_ID from '@angular/core' 1 从“@angular/core”导入 PLATFORM_ID

import { ..., PLATFORM_ID } from '@angular/core';

2 Inject in the component constructor where you have to init the AOS 2 注入必须初始化 AOS 的组件构造函数

constructor(..., @Inject(PLATFORM_ID) private platformId: Object)

3 Wrap the AOS.init with this if statement: 3 使用以下 if 语句包装 AOS.init:

if (isPlatformBrowser(this.platformId)) { AOS.init({ once: true, duration: 1000 }); })

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

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