【发布时间】:2018-07-21 11:05:07
【问题描述】:
我正在开发 Outlook Web Addin,尝试使用 Angular 5 使其工作,我已遵循 microsoft 提供的官方文档。
https://docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/excel-quickstart-angular
是的,上面的链接适用于 excel,但我可以找到类似的基本设置。 我已经在 main.ts 中初始化了 Office,比如
main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
try{
Office.initialize = function () {
platformBrowserDynamic().bootstrapModule(AppModule);
};
}catch(err){
}
我在这个插件中使用路由,它在 app.module.ts 中声明和设置,代码是
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { AppComponent } from './app.component';
import { ArchiveComponent } from './archive/archive.component';
import { SearchComponent } from './search/search.component';
@NgModule({
declarations: [
AppComponent,
ArchiveComponent,
SearchComponent
],
imports: [
BrowserModule,
HttpModule,
RouterModule.forRoot([
{
path:'archive',
component:ArchiveComponent
},
{
path:'search_archive',
component:SearchComponent
}
])
],
providers: [
{ provide: LocationStrategy, useClass: HashLocationStrategy },
],
bootstrap: [AppComponent]
})
export class AppModule { }
我已经在本地测试了路由 /#archive,它可以正常工作并在 ArchiveComponent 模板页面中显示 html。仅当在 Office.initailize 中未完成引导时。
当我在我的 apache 服务器上托管插件并将其安装在我的 Outlook 网络中时,它仅第一次工作。 ArchiveComponent 组件代码如下:
import { Component, OnInit } from '@angular/core';
declare const Office: any;
@Component({
selector: 'app-archive',
templateUrl: './archive.component.html',
styleUrls: ['./archive.component.css']
})
export class ArchiveComponent implements OnInit {
constructor(private zone: NgZone) { }
ngOnInit() {
console.log(Office.context.mailbox.item.itemId)
}
}
oninit 方法中的控制台在第一次打开时从 Outlook Web 的浏览器中打印,之后当我下次在同一个 Outlook Web 上重新打开插件时,它会引发错误
Office.js has not fully loaded. Your app must call "Office.onReady()" as part of it's loading sequence (or set the "Office.initialize" function). If your app has this functionality, try reloading this page.
下面是来自控制台的图像,有 2 个错误,第一个是未知的,但它来自插件路由,第二个是第一次打印
默认 Angular 版本构建的相同插件效果很好,但在尝试将其迁移到 Angular 5 时遇到问题。
我可以猜到我在 Office.initialize 中缺少一些东西,但不确定。
迁移的原因是在加载项中使用路由,以便为 2 个不同的加载项创建 2 个项目。
【问题讨论】:
-
在调试加载项时,您能否检查每次加载加载项时是否调用了“Office.initialize = ...”语句? office.js javascript 错误表明它是未定义的。
-
在做了一些练习后,我发现当我打开同样的IE浏览器时,pollyfill.js中也出现了一些错误。我取消了 pollyfill.ts 中的一些行的注释以添加对浏览器的支持,并且一切正常。
标签: angular outlook-addin office-addins outlook-web-addins