angular ssr处理服务器不存在对象处理方式

  • document 
    export class ExampleComponent { constructor(@Inject(DOCUMENT) document: any) { console.dir(document) } }

  • window 
    const WINDOW = new InjectionToken('window', { providedIn: 'root', factory: () => window }); 
    
    or 
    
    export class ExampleComponent { constructor(@Inject(WINDOW) window: any) { console.dir(window) } }
    Location 
    
    import { Location } from '@angular/common';
    
    export class AbmNavbarComponent implements OnInit{
    
      // ctor 中注入 Location
    
      constructor(private _location:Location){
    
        //...
    
      }
    
      ngOnInit() {
    
        // 打印当前地址
    
        console.log(this._location.path(true));
    
      }
    
    }
    

你可能感兴趣的:(前端,javascript,angular)