问题描述
基本上,我在调用Google Analytics(分析)API以获得核心报告数据时收到以下错误.它可以在我的本地主机服务器上运行,但是当我尝试部署该应用程序时,它对我来说失败.请提供有关如何在angular2 +中导入"gapi"变量的建议.非常感谢!
Basically, I am receiving the error below when I call the google analytics api for core reporting data. It works on my localhost server but when I try to deploy the app, it fails for me. Please advise on how to import the "gapi" variable in angular2+. Many thanks!
这就是我在我的有角度的应用程序中如何称呼它的原因. gapi.auth.authorize(authData,(res:any)=> {})
This is how I call it in my angular app.
gapi.auth.authorize(authData, (res:any)=>{})
ERROR:错误TS2304:无法 找到名称"gapi". src/app/order/order.component.ts(66,11):错误TS2304:找不到 名称为"gapi". src/app/order/order.component.ts(67,11):错误TS2304:找不到 名称为"gapi". src/app/order/order.component.ts(69,13):错误TS2304:找不到 名称为"gapi". src/app/order/order.component.ts(71,15):错误TS2304:找不到 名称为"gapi". src/app/order/order.component.ts(77,17):错误TS2304:找不到 名称为"gapi".
ERROR in src/app/order/order.component.ts(65,7): error TS2304: Cannot find name 'gapi'. src/app/order/order.component.ts(66,11): error TS2304: Cannot find name 'gapi'. src/app/order/order.component.ts(67,11): error TS2304: Cannot find name 'gapi'. src/app/order/order.component.ts(69,13): error TS2304: Cannot find name 'gapi'. src/app/order/order.component.ts(71,15): error TS2304: Cannot find name 'gapi'. src/app/order/order.component.ts(77,17): error TS2304: Cannot find name 'gapi'.
下面是我的代码
gapi.auth.authorize(authData, (res:any)=>{
gapi.client.load('analytics', 'v3').then(function() {
gapi.client.analytics.management.accounts.list().then( (accountResponse:any) =>{
let accountId = accountResponse.result.items[4].id;
gapi.client.analytics.management.webproperties.list({'accountId': accountId})
.then((accountPropertiesResponse:any) => {
gapi.client.analytics.management.profiles.list({
'accountId': accountPropertiesResponse.result.items[0].accountId,
'webPropertyId': accountPropertiesResponse.result.items[0].id,
})
.then((profileIdResponse:any)=>{
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileIdResponse.result.items[0].id,
'start-date': sevenDaysAgo,
'end-date': databaseDate,
'metrics': 'ga:sessions',
'dimensions': 'ga:deviceCategory',
}).then((coreReportResponse:any)=>{
mobileNum = coreReportResponse.result.rows[1][1];
desktopNum = coreReportResponse.result.rows[0][1];
tabletNum = coreReportResponse.result.rows[2][1];
visits = coreReportResponse.result.totalsForAllResults['ga:sessions'];
});
});
});
});
});
});
index.html
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ShopifyReport</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script src="https://apis.google.com/js/client.js?onload=authorize"
async defer></script>
</body>
</html>
tsconfig.json
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types",
],
"types": ["gapi", "gapi.auth2", "gapi.auth"],
"lib": [
"es2017",
"dom"
]
}
}
推荐答案
向组件添加了以下代码,并允许从Google Analytics(分析)进行访问以进行部署链接.谢谢大家!
Added the following code to the component and allowed access from Google Analytics for deploy link. Thank you all!
declare var gapi : any;
这篇关于Angular 2+错误:找不到名称"gapi"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!