本文介绍了如何在Angular 2(Azure-AD)的webpack中加载adal.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的NG2项目中,如果我这样写:

Inside my NG2 project, if I write:

import adal from 'adal-angular'; 

它给了我 adal is undefined。为什么?

it gives me 'adal is undefined'. Why is that?

我想在我的angular 2项目中使用adal.js。

我已经运行了

I want to use adal.js inside my angular 2 project.
I already have run

npm install adal-angular --save
npm install @types/adal --save

然后,在我的component.ts文件中,如果这样做:

Then, inside my component.ts file, if I do:

从 adal-angular进口adal;

import adal from 'adal-angular';

adal未定义。



With those packages installed, you have to do the following:
inside your component.ts:


  1. 写一个三元组斜杠以导入类型

    ///<参考路径= ../../../ node_modules/@types/adal/index.d.ts / >

  2. 导入adal.js并使用暴露加载器将其作为AuthenticationContext公开

    import'expose? AuthenticationContext!../../../ node_modules / adal-angular / lib / adal.js';

  3. 声明一个类型为AuthenticationContextStatic的变量,并且将其签名AuthenticationContext的值

    let createAuthContextFn:adal.AuthenticationContextStatic = AuthenticationContext;

  4. 现在您可以使用createAuthContextFn

    let config初始化身份验证上下文:adal.Config = {clientId:'test'};
    let context = new createAuthContextFn(config);

  1. write a triple slash to import the typings
    /// <reference path="../../../node_modules/@types/adal/index.d.ts" />
  2. import adal.js and expose it as AuthenticationContext using the expose loader
    import 'expose?AuthenticationContext!../../../node_modules/adal-angular/lib/adal.js';
  3. declare a variable of type AuthenticationContextStatic and assign it value of AuthenticationContext
    let createAuthContextFn: adal.AuthenticationContextStatic = AuthenticationContext;
  4. Now you can initialize the authentication context using the createAuthContextFn
    let config: adal.Config = { clientId : 'test' };let context = new createAuthContextFn(config);

(可选)以处理来自AD,将这段代码写在您的自举组件(也称为AppComponent)内:

if(context.isCallback(location.hash)){
var requestInfo = context.getRequestInfo( location.hash);
context.saveTokenFromHash(requestInfo);
}

(Optionally) To handle the callback from AD, write this piece of code inside your bootstrapped component aka AppComponent:
if (context.isCallback(location.hash)) { var requestInfo = context.getRequestInfo(location.hash); context.saveTokenFromHash(requestInfo);}

这篇关于如何在Angular 2(Azure-AD)的webpack中加载adal.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:19