找不到名称“办公室" [英] Cannot find name 'Office'
问题描述
我正在将Angular 4与Office.js一起使用.该项目由Angular CLI创建.
I am using Angular 4 with Office.js. The project is created by Angular CLI.
代码很简单:
// declare const Office: any;
// With the line above, the app runs perfect
Office.initialize = function () {
platformBrowserDynamic().bootstrapModule(AppModule);
};
我得到了错误
找不到名称"Office".
Cannot find name 'Office'.
我已经做了npm install --save-dev @types/office-js
我的tsconfig.json文件:
My tsconfig.json file:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
}
}
更新1:
在npm install --save @microsoft/office-js
之后,遵循@MahmoodSajjadi的建议并使用
Following @MahmoodSajjadi suggestion, after npm install --save @microsoft/office-js
, and use
import { Office } from '@microsoft/office-js';
收到此错误:
/my-app/src/main.ts中的ERROR(3,24):文件 "/my-app/node_modules/@types/office-js/index.d.ts"不是一个模块.
ERROR in /my-app/src/main.ts (3,24): File '/my-app/node_modules/@types/office-js/index.d.ts' is not a module.
ERROR找不到模块:错误:无法解决 '/my-app/src'中的'office-js'
ERROR in ./src/main.ts Module not found: Error: Can't resolve 'office-js' in '/my-app/src'
@ ./src/main.ts 3:0-35
@ multi webpack-dev-server/client? http://localhost:4200 ./src/main.ts>
@ ./src/main.ts 3:0-35
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
在package.json中,实际上它是"@microsoft/office-js": "0.0.0"
,不确定这是一个正确的软件包.
In package.json, actually it is "@microsoft/office-js": "0.0.0"
, not sure this is a correct package.
更新2:
根据Michael的回答,似乎NPM版本尚未准备好,我将首先使用CDN版本.谢谢大家的帮助!
Based on Michael's answer, seems NPM version is not ready, I will stay with CDN version first. Thanks for everyone's help!
推荐答案
似乎您仍然需要指定应使用office-js
类型.打开src/tsconfig.app.json
并将office-js
添加到类型数组,如果它以前是空数组,则应该如下所示:
It looks like you still need to specify that the office-js
types should be used. Open up src/tsconfig.app.json
and add office-js
to the types array, which should then look like this if it was previously an empty array:
"types": [
"office-js"
]
接下来,您需要在项目目录中运行命令tsc -p tsconfig.json
,然后再尝试再次构建项目.
Next, you need to run the command tsc -p tsconfig.json
from within your project directory before attempting to build the project again.
这篇关于找不到名称“办公室"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!