我正在使用Angular 4與Office.js。該項目由Angular CLI創建。找不到名字'Office'
代碼很簡單:
// declare const Office: any;
// With the line above, the app runs perfect
Office.initialize = function() {
platformBrowserDynamic().bootstrapModule(AppModule);
};
我
Cannot find name 'Office'.
我已經做了錯誤npm install --save-dev @types/office-js
我tsconfig.json文件:
{
"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:
繼@MahmoodSajjadi建議,npm install --save @microsoft/office-js
後,用
import { Office } from '@microsoft/office-js';
得到這個錯誤:
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 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
在的package.json,實際上它是"@microsoft/office-js": "0.0.0"
,不知道這是一個正確的方案。
更新2:
基於邁克爾的回答,似乎NPM的版本還沒有準備好,我會留在CDN版本第一。感謝大家的幫助!
您需要聲明Office或導入它(作爲您的代碼註釋) –
@MahmoodSajjadi如何正確導入?我嘗試從'office-js'導入{Office};'但是不起作用。 –
包'@ type/office-js'只是打包包而不是實際的'office.js',你需要'npm i @ microsoft/office-js' –