在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
描述依赖注入是一个用来管理代码依赖的强大模式。 依赖注入是一种将对象作为应用程序中不同组件中的依赖关系传递的设计模式。 它创建一个新的类实例及其所需的依赖项。 依赖注入被刺激到框架中,并且可以在任何地方使用。 使用依赖注入时,必须记住以下几点:
例子下面的例子描述了在Angular 2中使用依赖注入: <!DOCTYPE html> <html> <head> <title>Angular 2 Dependency Injection</title> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/es6-shim.min.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/typescript.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/Rx.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'app': {defaultExtension: 'ts'}} }); System.import('/angular2/src/app/dependency_injection_main') .then(null, console.error.bind(console)); </script> </head> <body> <my-app>Loading...</my-app> </body> </html> 上述代码包括以下配置选项:
让我们创建TypeScript(.ts)文件并将它们保存在app文件夹中。 dependency_injection_main.tsimport {bootstrap} from 'angular2/platform/browser'; //importing bootstrap function import {AppComponent} from "./fruit-component"; //importing component function bootstrap(AppComponent);fruit-component.ts import {Component} from 'angular2/core'; import {MyListComponent} from './play-component'; //@Component is a decorator that uses configuration object to create the component and its view. @Component({ selector: 'my-app', //The selector creates an instance of the component where it finds 'my-app' tag in parent HTML template:`<my-list></my-list>`, directives:[MyListComponent] //'MyListComponent' is the root component of fruits that governs child components }) export class AppComponent { }play-component.ts import {Component} from "angular2/core"; import {FruitService} from "./fruits.service"; import {Fruits} from "./fruits"; import {OnInit} from "angular2/core"; @Component({ selector: "my-list", template: ` List of Fruits<br> <ul> <li *ngFor="#list of fruits"> //The NgFor directive instantiates a template once per item from an iterable {{list.id}} - {{ list.name }} </li> </ul> `, providers: [FruitService] //providers are part of @Component metadata }) //The 'MyListComponent' should get list of fruits from the injected 'FruitService' export class MyListComponent implements OnInit { public fruits : Country[]; //Using constructor, call the _fruitService and populate the fruits list constructor(private _fruitService: FruitService) {} getContacts(){ this._fruitService.getContacts().then((fruits: Country[]) => this.fruits = fruits); } //The 'ngOnInit()' hook is called when done with creating the component and evaluated the inputs ngOnInit():any{ this.getContacts(); } }fruits.service.ts import {Injectable} from "angular2/core"; import {COUNTRIES} from "./fruits.lists"; //It is used for meta data generation and specifies that class is available to an injector for instantiation @Injectable() //'FruitService' exposes 'getContacts()' method that returns list of data export class FruitService { getContacts() { return Promise.resolve(COUNTRIES); // takes values from fruits.lists.ts file } }fruits.lists.ts import {Fruits} from "./fruits"; //storing array of data in Fruits export const COUNTRIES: Fruits[] =[ {"id": 1, name :"Apple"}, {"id": 2, name: "Grapes"}, {"id": 3, name: "Orange"}, {"id": 4, name: "Banana"} ];fruits.ts export interface Fruits{ id: number; name: string; } 输出让我们执行以下步骤,看看上面的代码如何工作:
或者你可以用另一种方式运行这个文件:
相关教程 |
请发表评论