首页 文章

Nativescript - 访问需要授权的img src

提问于
浏览
1

我试图在Nativescript中显示借助OAuth保护的图像 .

<Image src="url-to-secured-image"></Image>

所以我需要在请求中以某种方式将jwt标记添加到标头中 . 我环顾四周,找到了angular-img-http-src . 但这不适用于Angular2 . Angular2中默认是否支持这种方式?

2 回答

  • 2

    你可以使用插件https://github.com/NathanWalker/ng2-image-lazy-load

    // view template
    <div imageLazyLoadArea [imageLazyLoadConfig]="lazyLoadConfig">
      <div *ngFor="let image of images">
        <img [imageLazyLoadItem]="image.url"/>
      </div>
    </div>
    
    // Component
    public lazyLoadConfig: IImageLazyLoadConfig = {
      headers: {
        'Authorization': 'Bearer auth-token'
      },
      loadingClass: 'custom-loading',
      loadedClass: 'custom-loaded',
      errorClass: 'custom-error'
    };
    
  • 2

    我找到了一个简单的解决方案 . 由于我使用的是NativeScript,因此我可以使用HttpModule.getImage()从后端获取带有身份验证标头的图像 . 这是一个例子:https://www.thepolyglotdeveloper.com/2016/02/use-the-http-module-instead-of-fetch-in-nativescript/

    然后我将生成的imageSource粘贴到html代码中的Image.src .

相关问题