angular - Office Word Addin:在办公环境中调用 Angular 6 服务时出现空值或未定义错误
问题描述
我正在使用 Angular 6 开发 word officejs 应用程序。在 Office.context.document 中调用 this.anyservice 或 this.anyvariable 时出错。
服务是,
@Injectable()
export class OfficeService {
constructor(private ngxXml2jsonService: NgxXml2jsonService) {
}
ReadData(){
Office.context.document.customXmlParts.getByNamespaceAsync("namespace", function (asyncResult) {
this.ngxXml2jsonService = asyncResult
});
}
}
我将结果 Office.context.document 分配给 this.ngxml2jsonService。
出现错误: 无法设置未定义或空引用的属性“ngxml2jsonService”
即使我试图调用另一个服务,但仍然得到同样的错误。然后我打印console.log(this),所以才知道“this”在office.context.document中也无法访问。
我需要帮助来解决这个问题。
解决方案
在调用 office.context 方法之前,您需要在其他变量中维护此实例...
像这样的东西:
ReadData() {
var self = this;
Office.context.document.customXmlParts.getByNamespaceAsync("namespace", function (asyncResult) {
self.ngxXml2jsonService = asyncResult
});
}
这是必需的,因为 getByNamespaceAsync 将调用您的 callback() 函数。
推荐阅读
- codeigniter - 如何解决 codeigniter 购物车更新问题
- java - 如何获取xml节点?
- sql - 如何在sql查询中对一个字段中的多个字段求和?
- ios - swift:如何打开 Safari 应用程序(不是其中的链接)
- vue.js - 隐藏多部分选项表单的值 - Vue
- angular6 - 对 Angular 6 单页应用程序实施 Azure AD 单点登录
- python - 如何解决networkx2.2,python2.7中的这个字典错误?
- azure - 捕获发送到 Azure 服务总线主题但没有订阅或过滤掉的消息?
- javascript - 当我需要存储和重新显示用户选择的词汇和短语的信息时,如何处理一个笔记网站?
- python-3.x - 为什么我无法使用 kvaser CAN-USB 连接器接收来自 CAN 消息的数据?