简体   繁体   English

Aurelia的角度服务?

[英]Angular Service in Aurelia?

I've yet to find decent documentation detailing how to migrate from Angular 1.x to Aurelia. 我还没有找到详细介绍如何从Angular 1.x迁移到Aurelia的文档。 So far, I've only seen folks detailing how the concept of an Angular directive can be remade in Aurelia using @customElement . 到目前为止,我只看到人们详细介绍了如何使用@customElement在Aurelia中重新创建Angular directive的概念。 Okay, simple enough. 好的,很简单。 But these examples always, always just mock data. 但这些例子总是只是模拟数据。

That said, Angular Services are singletons that can be injected into any controller/directive/service, and typically allows for the fetching of data from a server (ie PersonService , OrdersService ). 也就是说,Angular Services是可以注入任何控制器/指令/服务的单例,通常允许从服务器(即PersonServiceOrdersService )获取数据。

But how are these data services modeled in Aurelia? 但这些data services如何在Aurelia建模? Is everything just a class? 一切都只是一个阶级? It seems like it . 看起来好像

Essentially, I'd to see some code samples, a hello-world , that effectively fetches data from a service, and provides it to a @customElement . 本质上,我将看到一些代码示例,一个hello-world ,它可以有效地从服务中获取数据,并将其提供给@customElement Where do the HTTP calls go? HTTP调用在哪里? How do we even make HTTP calls? 我们如何进行HTTP调用? Angular uses $http , what about Aurelia? Angular使用$http ,Aurelia怎么样?

EDIT : 编辑

Here's a simple angular service. 这是一个简单的角度服务。 How would one attack this in Aurelia? 怎么会在奥里利亚攻击这个?

app.service('SomeDataService', function () {
    return {
        getMyData: function (options) {
            return $.ajax(options);
        }
    }
});

Yep- plain ES6/ES7 classes. 是的ES6 / ES7课程。 There's no framework intrusion in your data services. 您的数据服务没有框架入侵。

my-data-service.js 我的数据,service.js

import {HttpClient} from 'aurelia-http-client'; // or 'aurelia-fetch-client' if you want to use fetch
import {inject} from 'aurelia-framework';

@inject(HttpClient)
export class MyDataService {
  constructor(http) {
    this.http = http;
  }

  getMyData() {
    return this.http.get(someUrl);
  }
}

fancy-custom-element.js 花式定制element.js

import {MyDataService} from './my-data-service';
import {inject} from 'aurelia-framework';

@inject(MyDataService) // aurelia's dependency injection container will inject the same MyDataService instance into each instance of FancyCustomElement
export class FancyCustomElement {
  data = null;

  constructor(dataService) {
    this.dataService = dataService;
  }

  // perhaps a button click is bound to this method:
  loadTheData() {
    this.dataService.getMyData()
      .then(data => this.data = data);
  }
}

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

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