如何修复declare var error:typescript上的意外标记

时间:2017-05-05 18:39:16

标签: javascript typescript

我试图在JS文件中调用导入的函数(请参阅src =" https://cdns.gigya.com/JS/gigya.js?apiKey = 3_sVidf29tz") ts文件使用"声明var gigya;"但我收到一个错误:意外的令牌

我错过了什么?

我的代码: JS login.component.html

<body onload="callScreenSet()">
<div id="screen-set"></div>
</body>

的index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LanguageApp</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="/styles.css" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" lang="javascript" src="https://cdns.gigya.com/JS/gigya.js?apiKey=3_sVidf29tz"></script>

</head>

<body>
  <app-root>Loading...</app-root>
</body>
</html>

login.component.ts

import { Component, OnInit, AfterContentInit } from '@angular/core';
import any = jasmine.any;
@Component({
  selector: 'app-root',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']  
})
    declare var gigya;
export class LoginComponent implements OnInit {  
  private _screenSet: string = 'Default-RegistrationLogin';
  private _containerID:string = 'screenSet';
  constructor(private srvLogin: LoginService) {}
  ngOnInit() {}  
  public callScreenSet():void
  {
    gigya.accounts.showScreenSet({screenSet: this._screenSet,containerID: this._containerID});
  }
}

的package.json

{
  "name": "language-app",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.2.1",
    "@angular/compiler": "2.2.1",
    "@angular/core": "2.2.1",
    "@angular/forms": "2.2.1",
    "@angular/http": "2.2.1",
    "@angular/platform-browser": "2.2.1",
    "@angular/platform-browser-dynamic": "2.2.1",
    "@angular/router": "3.2.1",
    "auth0-lock": "^10.14.0",
    "core-js": "^2.4.1",
    "rxjs": "5.0.0-beta.12",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angular/compiler-cli": "2.2.1",
    "@types/jasmine": "2.5.38",
    "@types/lodash": "4.14.50",
    "@types/node": "^6.0.70",
    "angular-cli": "1.0.0-beta.21",
    "codelyzer": "~1.0.0-beta.3",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "~2.0.3",
    "webdriver-manager": "10.2.5"
  }
}

由于

1 个答案:

答案 0 :(得分:2)

您的代码:

@Component({
  selector: 'app-root',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']  
})
    declare var gigya;
export class LoginComponent implements OnInit {  
装饰者declare后出现

@Component。简而言之,语法错误就像1***2一样,会出现语法错误。修复

declare var gigya;
@Component({
  selector: 'app-root',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']  
})
export class LoginComponent implements OnInit {