[英]AOS (animate on scroll) library not working when selecting elements to animate with jQuery
[英]Initialise AOS - Animate on scroll library in Angular Universal Server Side Rendering
我已經在我的項目中實現了 AOS - Animate on scroll library 來處理動畫。 但是當我將我的 Angular 項目轉換為 Angular 通用時。 錯誤開始彈出。
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)
我找不到錯誤未定義的文檔。
請如果有人可以照亮它。
以下是我在組件中實現 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 });
}
}
我不確定是否有警告和解決方法,但我可以通過使用來自@angular/common 的 Angular "isPlatformBrowser" 包裝它來繞過錯誤。
這是解決方案:
1 從“@angular/core”導入 PLATFORM_ID
import { ..., PLATFORM_ID } from '@angular/core';
2 注入必須初始化 AOS 的組件構造函數
constructor(..., @Inject(PLATFORM_ID) private platformId: Object)
3 使用以下 if 語句包裝 AOS.init:
if (isPlatformBrowser(this.platformId)) { AOS.init({ once: true, duration: 1000 }); })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.