Word加载项对话框不反映从父级设置的localstorage值

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

我在word加载项中使用office对话框api。当我在打开后设置localstorage并在对话框中读取时,我在对话框中看到了值。但是当对话框打开并且我在父级中设置localstorage时,我没有看到它出现在对话框中。我在启动后使用localstorage与子对话框进行通信。对话框打开后,父/子之间是否还有其他通信方式?从对话框向父级发送消息时使用Office.context.ui.messageParent。

Main page:

$scope.opensettingDialog = function () {
Office.context.ui.displayDialogAsync("https://localhost/tz/setting", {90,50},
               function (asyncResult) {
                   dialog = asyncResult.value;
            dialog.addEventHandler(Office.EventType.DialogMessageReceived, processSettingDialog);
        });}

function processSettingDialog (arg) {
    var messageFromDialog = JSON.parse(arg.message);
    if (messageFromDialog.messageType == "save") {
        //do operations & store result
        localStorage.setItem("tbSave", "true");
    }
    else {
        dialog.close();
    }
}


Settings Dialog:

$scope.acceptSettings = function () {

    var messageObject = {
        messageType: "savedoc"
    };
    var jsonMessage = JSON.stringify(messageObject);
    Office.context.ui.messageParent(jsonMessage);

    $scope.intervalpromise = $interval(checkSave2Update(), 1000);


};

//Wait for parent operation to complete
var checkSave2Update= function () {
    var processingStatus = localStorage.getItem("tbSave");//it never has the value set in the parent page.

    if ((processingStatus == "undefined") || (processingStatus == null)) {
        $interval.cancel($scope.intervalpromise);
        $scope.intervalpromise = $interval(checkSave2Update(), 1000);
        return;
    }
    else {//cancel wait
        $interval.cancel($scope.intervalpromise);
    }
}
ms-word office-js add-in
1个回答
0
投票

就localStorage而言,可能只是因为您遇到了Internet Explorer问题(在桌面上,在IE容器内部运行Office Add-in)。

问题和解决方法在https://stackoverflow.com/a/40770399中有详细描述。基本上,本地存储可能会在选项卡之间失去同步(这实际上是任务窗格与对话框的对比)。要解决此问题,您可以在某个键上设置一个值,这将确保刷新localStorage。

我们在Script Lab中使用此解决方法:请参阅https://github.com/OfficeDev/script-lab/blob/master/packages/common/src/utilities/ensure.fresh.local.storage.ts

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.