错误TypeError:this._http.request(...)。share不是函数

时间:2018-05-29 04:30:43

标签: angular

当我尝试授权时,我收到此错误:

  

ERROR TypeError:this._http.request(...)。share不是函数

     

在AuthService.request(auth.service.ts:569)

     

在AuthService.post(auth.service.ts:502)

     

在AuthService.signIn(auth.service.ts:166)

     

在SignInComponent.signIn(sign-in .component.ts:35)

     

at Object.eval [as handleEvent](SignInComponent.html:24)

     

在handleEvent(core.js:13547)

     

在callWithDebugContext(core.js:15056)

     

at Object.debugHandleEvent [as handleEvent](core.js:14643)

     

在dispatchEvent(core.js:9962)

     

at eval(core.js:10587)

我无法找到她的位置,谁可以帮忙。使用两个路由和两个模块。

以下是一些文件的列表。如果你需要扔别的东西,请写。虽然这里的所有列表都存在错误。

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { NgModule  } from '@angular/core';
import { HttpModule } from '@angular/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { ToastModule, ToastOptions } from "ng2-toastr";
import { ToastCustomOptions } from "./shared/toast-cutom-options";

import { AppComponent } from './app.component';

import { routing } from './routings/routing.module';

import { SignInModule } from './layout/sign-in/sign-in.module';
import { LayoutModule } from './layout/layout.module';

import { User } from "./models/user.model";
import { AuthGuard } from './shared/guards/auth.guard';
import { AuthService } from './services/auth/auth.service';
import { environment } from "../environments/environment";

declare let moment: any;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserAnimationsModule,
    BrowserModule,
    RouterModule,
    FormsModule,
    HttpModule,
    SignInModule,
    ReactiveFormsModule,
    LayoutModule,
    routing,
    ToastModule.forRoot()
  ],
  providers: [AuthService, AuthGuard,  {provide: ToastOptions, useClass: ToastCustomOptions} ],
  bootstrap: [AppComponent]
})
export class AppModule { 
  constructor(public authService: AuthService) {
    moment.locale('ru');

    this.authService.init({
      apiPath: environment.apiUrl,
      signInRedirect: 'sign_in',
      signInPath: '/sign_in',
      validateTokenFailedRedirect: 'sign_in',
      signOutFailedValidate: true,
      dataStorage: localStorage,
      userDataClass: User
    });
  }
}

router.module.ts:

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';

import { LayoutComponent} from '../layout/layout.component';

import { SignInComponent } from '../layout/sign-in/sign-in.component';

import { NotFoundComponent } from '../layout/not-found/not-found.component';

import { AuthGuard } from '../shared/guards/auth.guard';

export const routes: Routes = [
  { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'sign_in',   component: SignInComponent },
  { path: 'dashboard',   component: LayoutComponent },
  { path: 'not_found',   component: NotFoundComponent },
  { path: '**', component: NotFoundComponent }

];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes, { useHash: true });

2 个答案:

答案 0 :(得分:0)

让response = this._http.request(new Request(baseRequestOptions))。share();

baseRequestOptions应为object类型。

从您的代码中不清楚它是否是一个对象。 检查typeof(baseRequestOptions),如果它不是对象,则修改你的代码。

示例代码:

constructor(public http:Http) {}
  request(url:string) {
    return this.http.request(new Request({
     method: RequestMethod.Get,
     url: url,
     search: 'password=123'
  })).share();
}

答案 1 :(得分:0)

RxJS v5.5.2+已移至Pipeable运营商,以改善树木震动并更轻松地创建自定义运算符。  现在operators需要使用pipe方法合并 Refer This
新导入

import { share} from 'rxjs/operators';

示例

this.response$=this.http.get('url').pipe(share());

除此之外您正在使用已弃用的HttpModule ,您应使用 HttpClientModule 代替