【问题标题】:Angular SSR ERROR ReferenceError: Swiper is not definedAngular SSR ERROR ReferenceError: Swiper 未定义
【发布时间】:2022-01-24 19:16:05
【问题描述】:

我尝试创建一个新项目并向其中添加 SSR,它工作正常。

但是当我尝试将 SSR 添加到现有项目时,会出现此错误

【问题讨论】:

    标签: javascript angular npm server-side-rendering angular-universal


    【解决方案1】:
    import { isBrowser } from 'angular2-universal';
      
      or 
    
    // app.browser
    @NgModule({
      providers: [
        { provide: 'isBrowser', useValue: true }
      ]
    })
    
      
       
    then inject from constructor
    
    export class SomeComponent implements OnInit {
      constructor(@Inject('isBrowser') private isBrowser: boolean)
      ngOnInit() { 
        // example usage, this could be anywhere in this Component of course
        if (this.isBrowser) { 
          alert('we're in the browser!');
        }
    }
    
    
    <ng-container *ngIf="isBrowser">
       <!-- mqttws31-component -->
       <mqttws31-component></mqttws31-component> 
    </ng-container>
    

    【讨论】:

    • 感谢您的快速回放,但我仍然收到错误 swiper is not defined。
    • 很可能你在你的项目中使用了 Swiper,它不关心平台(在这种情况下是服务器)。在服务器上时,您不必执行任何 Swiper 的逻辑,使用类似于 @Kunjan 已经提出的技术。
    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 2021-03-19
    • 2018-04-16
    • 2020-06-20
    • 1970-01-01
    • 2018-12-09
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多