3

我有一个平均堆栈网站。最初,views/index.html(have <ui-view></ui-view>) 是所有页面的入口点。它使用angular-ui-routerhtml5mode。因此,https://localhost:3000/XXXX在浏览器中将保持不变(而不是添加#)并根据路由器显示相应的内容。

然后,我希望服务器也提供 Office 加载项。我将需要views/addin.html该 containsoffice.js和另一个路由器,以便该站点提供一组 urls https://localhost:3000/addin/XXXX,我不介意它是否html5mode存在(一个 url 可以包含#某处)。

所以这里是对应的routes/index.js

router.get('/addin/*', function (req, res) {
    console.log("router.get /addin/*");
    res.sendfile('./views/addin.html')
});

router.get('*', function(req, res) {
    console.log("router.get *");
    res.sendfile('./views/index.html');
});

这是views/addin.html

<html>
<head>
    <title>F-addin</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.2/angular-ui-router.js"></script>
    <!--<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>-->
    <base href="/" />
</head>
<body ng-app="addinF">
    addin content
    <ui-view ng-cloak></ui-view>
</body>
<script>
    var addinApp = angular.module('addinF', ['ui.router'])
    addinApp.config(['$stateProvider', '$locationProvider', function ($stateProvider, $locationProvider) {
        $stateProvider
            .state('addinNew', {
                url: '/addin/new',
                template: "new page"
            })
            .state('addinHome', {
                url: '/addin/home',
                template: "home page"
            })
        $locationProvider.html5Mode(true);
    }]);
</script>
</html>

什么时候office.js评论如上,https://localhost:3000/addin/new并且https://localhost:3000/addin/home在浏览器中运行良好。但是,当office.js取消注释时,会出现一个奇怪的行为:https://localhost:3000/addin/new在浏览器中输入会变成https://localhost:3000/#/addin/new,然后又变成https://localhost:3000/addin/new. 我们会看到addin content new page出现一次然后消失。

取消注释后,如果我们使用office.js加载清单文件<SourceLocation DefaultValue="https://localhost:3000/addin/new"/>,我们也会在任务窗格addin content new page中看到出现一次然后消失,然后Add-in Error Something went wrong and we couldn't start this add-in. Please try again later or contact your system administrator.

没有html5mode这里,.../addin/new或者.../addin/home在浏览器中只会显示addin content;加载项可以加载,也只能加载addin content

我的目标是制作一个指向.../addin/new.../addin/home工作的加载项。office.js必须在某处加载。我不介意它是否是html5mode(即,url 有#),但无论哪种方式的行为都是奇怪的或不令人满意的。有谁知道如何修复它或有解决方法?

4

1 回答 1

1

查看 office.js 文件的调试版本:

https://appsforoffice.microsoft.com/lib/1/hosted/office.debug.js

您会看到窗口的历史记录 replaceState 和 pushState 函数设置为 null:

window.history.replaceState = null;
window.history.pushState = null;

这禁用了操纵浏览器历史记录的能力,并且似乎 Angular 认为历史 API 不可用,因此 Angular 回退到标签导航。

office.js缩小版中,您可以通过以下方式找到这两行:

window.history.replaceState=e;window.history.pushState=e;

您可以删除这两行代码以重新启用 html5mode,但我不确定 office.js 插件是否会继续正常工作。

于 2017-07-01T07:07:49.553 回答