打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SAP Spartacus 里 对 isPlatformBrowser API 的使用

window-ref.ts 里的 isBrowser API,封装的是 Angular 标准 API,isPlatformBrowser:

来自 @angular/common:

 isBrowser(): boolean {    // TODO(#11133): Remove condition when platformId will be always provided    return this.platformId      ? isPlatformBrowser(this.platformId)      : typeof window !== 'undefined';  }

这是 @angular/common 里标准的 api:

https://angular.io/api/common/isPlatformBrowser

然后单击进去之后,看不到具体的实现代码:

@publicApi

export declare function, 其用法解释:

Declare vs. var

var creates a new variable. declare is used to tell TypeScript that the variable has been created elsewhere. If you use declare, nothing is added to the JavaScript that is generated - it is simply a hint to the compiler.

For example, if you use an external script that defines var externalModule, you would use declare var externalModule to hint to the TypeScript compiler that externalModule has already been set up

export declare class Action{...}

The class's real implementation is probably in somewhere else - maybe a .js file.

declare 提示了 Action 的实际实现一定在另一个位置,很可能是某个 .js 文件里。

TypeScript 帮助文档里对 declare 关键字的解释:

The TypeScript declare keyword is used to declare variables that may not have originated from a TypeScript file.

TypeScript 的 declare 关键字,用于声明一个变量,其原始定义可能并不是来自一个 TypeScript 文件。

For example, lets imagine that we have a library called myLibrary that doesn’t have a TypeScript declaration file and have a namespace called myLibrary in the global namespace. If you want to use that library in your TypeScript code, you can use the following code:

declare var myLibrary;

The type that the TypeScript runtime will give to myLibrary variable is the any type. The problem here is that you won’t have Intellisense for that variable in design time but you will be able to use the library in your code.

Another option to have the same behavior without using the declare keyword is just using a variable with the any type:

var myLibrary: any;
Both of the code examples will result in the same JavaScript output but the declare example is more readable and expresses an ambient declaration.

报错:uncaught referenceError: myLibrary is not defined

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ionic2+angular2中踩的那些坑
现代Web开发需要学习的15大技术
Angular、React 和 Vue 三大框架,Web 开发该如何选择?
Angular vs React:谁是2022年的获胜者
带你了解 Angular 与 Angular JS
JS中作用域和变量提升(hoisting)的深入理解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服