当前位置:  开发笔记 > 编程语言 > 正文

Aurelia的角度服务?

如何解决《Aurelia的角度服务?》经验,为你挑选了1个好方法。



1> Jeremy Danyo..:

是的ES6/ES7课程.您的数据服务没有框架入侵.

我的数据,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);
  }
}

花式定制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);
  }
}

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有