【发布时间】:2018-11-17 07:26:31
【问题描述】:
我是基于 JS 的 Office 插件的新手,我正在尝试使用 Angular 6 + Typescript 创建一个示例 Word 插件。
为此,我找到了一个看起来很完美的办公工具箱:https://www.npmjs.com/package/office-toolbox。然而,它的模板可以追溯到 Angular 1.x。因此,我尝试通过将 Angular 6 CLI 新项目与工具箱中的模板合并来进行实验,但没有成功。
我尝试了两种方法。当然,两者都需要先全局安装office工具箱:
npm install -g office-toolbox
加载项启动后,我的理解是我们必须先复制XML清单并在Word中打开它,即:
- 将 XML 清单从您的项目复制到您为此目的创建的共享中(例如
C:\Sideloads); - 启动插件(
npm start或ng serve); - 打开一个新的 Word 文档;
- 打开
Developer功能区并单击Add-ins; - 转到
Shared folder,选择您的插件,然后单击Add。功能区中会出现一个新按钮:单击它以显示其窗格。
问题在于,似乎不存在将 Angular 6 与 Office 插件结合使用的最新解决方案,并且了解此环境中的引导过程并非易事。所以我尝试了 2 种骇人听闻的方法,但都没有奏效。有人可以提出更好的方法吗?以下是方法:
方法 1
这遵循办公室工具箱网站的指示。
运行
office-toolbox generate并回答问题以基于 Angular 生成带有清单的新应用程序。现在我想尝试升级项目。我尝试通过将
package.json与ng new的默认包合并来更改package.json,然后运行npm install。工具箱生成的包长这样:
合并后的文件是:
{
"name": "sample-add-in",
"description": "",
"author": "",
"version": "0.1.0",
"scripts": {
"tsc": "tsc -p tsconfig.json -w",
"server": "browser-sync start --config bsconfig.json",
"copy": "cpx \"src/**/!(*.ts)\" dist --watch",
"start": "rimraf dist && concurrently \"npm run tsc\" \"npm run copy\" \"npm run server\"",
"validate": "./node_modules/.bin/validate-office-addin",
"ng": "ng",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26",
"office-ui-fabric-js": "^1.3.0",
"jquery": "^3.1.1",
"office-addin-validator": "^1.0.1"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.0",
"@angular-devkit/build-angular": "~0.6.0",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.0",
"@angular/language-service": "^6.0.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1",
"concurrently": "^3.1.0",
"cpx": "^1.5.0",
"rimraf": "^2.5.4",
"browser-sync": "^2.18.5",
"@types/office-js": "^0.0.37",
"@types/jquery": "^2.0.39",
"@types/angular": "^1.6.2"
}
}
然而,当我运行 npm start 时,我从主页收到这些错误:
Refused to apply style from 'https://localhost:3000/node_modules/angular/angular-csp.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
angular.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
Refused to execute script from 'https://localhost:3000/node_modules/angular/angular.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
app.ts:9 Uncaught ReferenceError: angular is not defined
at app.ts:9
at app.ts:43
o15apptofilemappingtable.debug.js:5530 Warning: Office.js is loaded outside of Office client
telemetryproxy.html:1 Failed to load resource: the server responded with a status of 404 ()
(index):1 Refused to apply style from 'https://localhost:3000/node_modules/angular/angular-csp.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
telemetryproxy.html:1 Failed to load resource: the server responded with a status of 404 ()
方法2
因此我尝试了相反的方法,恰好在https://github.com/Hongbo-Miao/office-addin-quick-start 上提出了建议(我不得不改变一些东西)。
创建一个新的 Angular 应用程序:
ng new sample-addin-
进入其目录,然后启动office工具箱:
office-toolbox。生成清单(创建子文件夹:否,创建新插件:否)。这将覆盖一些文件,因此在继续之前复制需要合并的文件:-
package.json:覆盖前复制。 -
tsconfig.json:不要覆盖,是一样的。 -
index.html:直接覆盖。
-
-
在
index.html中,在head结束标记之前添加: 在
main.ts中替换这个:
与:
声明 const 办公室:任何; Office.initialize = () => { platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.log(err)); };- 从已保存的 package.json 副本中合并缺失的内容,替换旧的 Angular 包和 Typescript 引用,并添加所有缺失的 Angular 相关包。示例:
- 如果您使用的是 Windows,由于加载项平台使用 Internet Explorer,请在
polyfills.ts中取消注释这些行:
这在加载时也会失败并出现类似错误。
【问题讨论】:
标签: angular typescript office-js