大佬教程收集整理的这篇文章主要介绍了angular – 使用另一个自定义服务的服务时没有提供…错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
基本服务看起来像这样
import {Injectable,ObservablE} from 'angular2/core'; import {http} from 'angular2/http'; import {requestOptionsArgs,Connection,ConnectionBACkenD} from 'angular2/http'; import {BaserequestOptions,requestOptions} from 'angular2/http'; import {requestMethods} from 'angular2/http'; import {ResponsE} from 'angular2/http'; import {request} from 'angular2/http'; import {makeTypeError} from 'angular2/src/facade/exceptions'; @Injectable() export class MyOAuth extends http { constructor(BACkend: ConnectionBACkend,defaultOptions: requestOptions) { super(BACkend,defaultOptions); } /** * Performs any type of http request. First argument is required,and can either be a url or * a {@link request} instance. If the first argument is a url,an optional {@link requestOptions} * object can be provided as the 2nd argument. The options object will be merged with the values * of {@link BaserequestOptions} before performing the request. */ request(url: String | request,options?: requestOptionsArgs): Observable<Response> { var responSEObservable: any; if (isString(url)) { responSEObservable = httprequest( this._BACkend,new request(mergeOptions(this._defaultOptions,options,requestMethods.Get,url))); } else if (url instanceof request) { responSEObservable = httprequest(this._BACkend,url); } else { throw makeTypeError('First argument must be a url String or request instance.'); } return responSEObservable; } }
使用上述服务的其他服务看起来像这样(authenticate.js):
import {Injectable,Inject} from 'angular2/angular2'; import {MyOAuth} from './MyOAuth'; @Injectable() export class Authenticationservice { constructor(@Inject(MyOAuth) http) { this.http = http; } authenticate(username,password,community) { console.log('authenticate'); } }
然后在课堂上使用这个服务我称之为:
import {PagE} from 'ionic/ionic'; import './login.scss'; import {AuthenticationservicE} from '../../services/authentication'; @Page({ templateUrl: 'app/login/login.html',providers: [Authenticationservice] //As of alpha 42 })
我在浏览器中得到的错误是
EXCEPTION: No provider for MyOAuth! (LoginPage -> Authenticationservice -> MyOAuth)
对我来说,我必须导入MyOAuth …
你可以做:
import {PagE} from 'ionic/ionic'; import './login.scss'; import {AuthenticationservicE} from '../../services/authentication'; @Page({ templateUrl: 'app/login/login.html',providers: [[Authenticationservice],[MyOAuth]] })
对我来说,如果服务可以在多个地方重用,我总是在应用程序上下文中定义它.例如:
@Component({ templateUrl: 'build/app.html',[MyOAuth]] }) class MyApp{ }
所以这些服务只会被安置一次.如果你提供它们,例如:Page1,Page2,它们将被实例化两次
以上是大佬教程为你收集整理的angular – 使用另一个自定义服务的服务时没有提供…错误全部内容,希望文章能够帮你解决angular – 使用另一个自定义服务的服务时没有提供…错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。