Getting Error - Cannot find name 'angular'

前端 未结 7 1528
野的像风
野的像风 2020-12-15 17:20

I\'m starting writing AngularJS app using TypeScript. I have this simple module command:

(() => {
    angular
        .module(\'app\', []);
})();
<         


        
相关标签:
7条回答
  • 2020-12-15 17:34

    Using AngularJS 1.x and TypeScript 2.x, I solved this issue by running:

    npm install --save-dev @types/angular
    

    and then include the following line on top of the .ts file:

    import * as angular from "angular";
    

    Reference: The Future of Declaration Files

    0 讨论(0)
  • 2020-12-15 17:37

    You could simplistically tell the compiler to stop worrying by telling it that "you know all about angular":

    declare var angular: any;
    

    To get all the good tooling and type checking, you need to pull in the Angular type definition so the compiler knows what is available.

    The type definitions are available via NuGet if you are using Visual Studio or various other methods for your favourite IDE.

    0 讨论(0)
  • 2020-12-15 17:42

    ATOM

    In tsconfig.json file ensure your "fileGlobs" points to both:-

    1. The TypeScript Definitions you installed with tsd &
    2. Your src .ts files
    "filesGlob": [
      "./typings/**/*.ts",
      "./src/client/**/*.ts"
    ]
    

    This will ensure you have tooling for the libraries you have installed (with tsd) without having to declare any variables. For example, if you tsd install angularjs --save, you can work angular in your src/../*.ts files with all the goody tooling.

    Hope this helps. Good Luck.

    0 讨论(0)
  • 2020-12-15 17:44

    You need to include angular.t.ds file in the library

    0 讨论(0)
  • 2020-12-15 17:50

    I solved the problem by adding the following line to my package.json, inside the "devDependencies" section:

    "@types/angular" : "1.6.17"
    

    after that, i just needed to run

    npm install
    

    in order for npm to download all the needed dependencies.

    0 讨论(0)
  • 2020-12-15 17:52

    For me inside of VisualStudio I used part of Augusto's suggested answer and simply did:

    npm install --save @types/angular
    

    and it worked like a charm.

    0 讨论(0)
提交回复
热议问题