简体   繁体   English

使用AngularJS在Typescript中导入模式?

[英]Import pattern for in Typescript with AngularJS?

This may be Typescript 101 but can't seem to get a clear answer for this. 这可能是Typescript 101,但似乎无法获得明确的答案。

I am new to Typescript and have come across some existing code (its AngularJS) that follows this format 我是Typescript的新手,遇到了一些遵循此格式的现有代码(其AngularJS)

module App.Login {
    import IStateService = angular.ui.IStateService;

    export class LoginController {

        private _state: IStateService;

        constructor($state: angular.ui.IStateService, ...) {
            this._state = $state;
        }
    }
}

Through reading around and some experimentation I discovered this can be rewritten in a far leaner manner as the following: 通过阅读和一些实验,我发现可以用以下更为精简的方式重写它:

module App.Login {  
    export class LoginController {

        constructor(private $state: angular.ui.IStateService, ...) {
        }
    }
}

My questions are: 我的问题是:

  • Why was it created the first way originally? 为什么最初创建第一种方法? Lack of knowledge or for some reason I don't understand 缺乏知识或出于某种原因我不了解
  • Why we need to use import just to alias the interface 为什么我们只需要使用import来别名接口
  • A question of style, but later in my class to use $state I have to use this.$state . 有关样式的问题,但是稍后在类中使用$state ,必须使用this.$state Coming from Angular v1 this feels odd, I presume this is ok though? 来自Angular v1,这感觉很奇怪,我想这可以吗?
  1. Why was it originally this way? 为什么原来是这种方式? If you mean the import, convenience. 如果您的意思是导入,那么方便。 angular.ui.IStateService is right on the edge of annoyingly long. angular.ui.IStateService就在令人讨厌的长边上。 They author probably just wanted a more convenient version. 他们的作者可能只是想要一个更方便的版本。 If you mean the declaration of the private var, some folks (especially coming from a C#/Java background) would prefer to declare class parameters explicitly. 如果您要声明私有变量,那么某些人(尤其是来自C#/ Java背景的人)宁愿显式声明类参数。 The visibility modifier ( public / protected / private ) on constructor parameters syntax is pretty unique to typescript so folks aren't used to it / don't like it. 构造函数参数语法的可见性修饰符( public / protected / private )对于打字稿而言非常独特,因此人们不习惯/不喜欢它。
  2. You don't. 你不知道 You can use the type keyword instead. 您可以改用type关键字。
  3. Yep, that's totally fine. 是的,那很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM