在Internet Explorer 11中使用reactjs和hellojs进行Sharepoint Framework

问题描述 投票:0回答:1

我有一个带有sharepoint框架和reactjs的webpart,我使用hellojs - > hellojs

我这样做:npm install hellojs --save并且在edge,chrome和firefox中都可以正常工作,但我需要在Internet Explorer 11中使用它。

我尝试使用js和html中的示例,并在Internet Explorer 11中正常工作,但在我的sharepoint项目中没有。我有这个:

import * as React from 'react';
import * as hello from 'hellojs';
import { Event } from '../interfaces/Event';

export class Authentication extends React.Component<{}, { sendEvent: boolean }> {
    private refreshTokenInterval: number;

    constructor(public props, public context) {
        super(props, context);
        this.state = {
            sendEvent: true
        };
    }

    public login(network) {
        hello.init({
            aad: {
                name: 'Azure Active Directory',

                oauth: {
                    version: 2,
                    auth: 'https://login.microsoftonline.com/tenant/oauth2/v2.0/authorize',
                    grant: 'https://login.microsoftonline.com/tenant/oauth2/v2.0/token'
                },

                // Authorization scopes
                scope: {
                    // you can add as many scopes to the mapping as you want here
                    profile: 'Group.Read.All',
                    offline_access: ''
                },

                scope_delim: ' ',

                login: (p) => {
                    if (p.qs.response_type === 'code') {
                        // Let's set this to an offline access to return a refresh_token
                        p.qs.access_type = 'offline_access';
                    }
                },

                base: 'https://www.graph.microsoft.com/v1.0/',

                get: {
                    me: 'me'
                },

                xhr: (p) => {
                    if (p.method === 'post' || p.method === 'put') {
                        JSON.parse(p);
                    } else if (p.method === 'patch') {
                        hello.utils.extend(p.query, p.data);
                        p.data = null;
                    }

                    return true;
                },

                // Don't even try submitting via form.
                // This means no POST operations in <=IE9
                form: false
            }
        });
        hello.init(
            {
                aad: 'clientId'
            },
            {
                redirect_uri: 'my redirect',
                //redirect_uri: 'https://localhost:4321/temp/workbench.html',
                scope: 'Group.Read.All'
            }
        );
        // By defining response type to code, the OAuth flow that will return a refresh token to be used to refresh the access token
        // However this will require the oauth_proxy server
        hello(network).login({ display: 'none' }).then(
            (authInfo) => {
                console.log(authInfo);
                localStorage.setItem('logged', authInfo.authResponse.access_token);
                localStorage.setItem('timeToRefresh', authInfo.authResponse.expires_in.toString());
                this.props.setEvent(Event.GET_ALL_GROUPS);
                this.setState({ sendEvent: false });
                clearInterval(this.refreshTokenInterval);
                this.refreshTokenInterval = window.setInterval(() => {
                    let timeToRefresh = Number(localStorage['timeToRefresh']) - 1;
                    localStorage.setItem('timeToRefresh', timeToRefresh.toString());
                    if (timeToRefresh <= 200) {
                        localStorage.clear();
                        sessionStorage.clear();
                    }
                }, 1000);
            },
            (e) => {
                console.error('Signin error: ' + e.error.message);
            }
        );
    }

    public componentDidMount() {
        let logged = localStorage['logged'];
        if (logged === undefined) this.login('aad');
        else {
            if (this.state.sendEvent) {
                this.props.setEvent(null);
                this.props.setEvent(Event.GET_ALL_GROUPS);
            }
        }
    }

    public render() {
        return null;
    }

    /*private logout(network) {
        // Removes all sessions, need to call AAD endpoint to do full logout
        hello(network).logout({ force: true }, console.log).then(
            function() {
                console.log('Out');
            },
            function(e) {
                console.error('Sign out error: ' + e.error.message);
            }
        );
    }*/
}

我在主类中称这个类:

public render(): JSX.Element {
        return (
            <div className="row">
                <div className="col-md-2" style={{ maxWidth: '250px' }}>
                    <LeftPanel setEvent={this.getEvent} />
                </div>
                <div className="col-md-10">
                    <Authentication setEvent={this.getEvent} />
                    <CenterPanel event={this.state.event} context={this.props.context} />
                </div>
            </div>
        );
    }

Internet Explorer 11中的控制台:

enter image description here

javascript reactjs sharepoint web-parts hello.js
1个回答
0
投票

解决了!

  1. cd“package.json的文件夹”
  2. npm install url-polyfill --save
  3. import'url-polyfill';
© www.soinside.com 2019 - 2024. All rights reserved.