服务对象的作用范围

1.高阶话题:服务对象的作用范围

声明服务提供者的方式:

方式1:在根模块中提供服务对象-----在整个应用中服务是单例

       @Injectable({ providedIn:'root' })

       export class TimerService{    }

方式2:在组件中提供服务对象----在每个组件实例中服务都是有一个实例的

       @Injectable()

       export class TimerService{    }

      ----------------------------------------

       @Component({

            .....

            providers:{ TimerService }

        })

       export class LoginComponent{       }

注意:项目中只要服务对象中有属性,只能用方式2;否则推荐使用方式1

       

2. 面试严重加分项:TypeScript

Angular从V2.x开始使用TS编写;Vue.js从V3开始使用TS编写;

中文网:http://www.tslang.cn

TS是由微软编写的一门基于JS又高于JS的编程语言。

浏览器不支持TS,其使用步骤:

1.下载TS的编译器

    npm i -g typescript

2.编写.ts文件,使用编译器转化为.js

    tsc  hello.ts

    //得到hello.js

TS学习手册:

https://www.tslang.cn/docs/handbook/basic-types.html

猜你喜欢

转载自www.cnblogs.com/sna-ling/p/12169037.html