首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角7拦截器

角7拦截器
EN

Stack Overflow用户
提问于 2019-06-26 07:53:52
回答 3查看 1.3K关注 0票数 1

我正在调用一个API,它将重定向到外部URL,执行身份验证,并再次重定向到我的主页,并在主页url中附加一些参数。

默认网址:https://mytesting.com

重定向网址后:https://mytesting.com/?someValue=123E234

当我重定向到默认页面(SomeValue)时,我想检索“https://mytesting.com/?someValue=123E234”。在java中,我们可以通过过滤器检索它。在角度上,我使用的是拦截器,但是拦截器永远不会被调用,也不知道如何使用拦截器来实现这一点。

共享/exampleAuth.拦截器。

代码语言:javascript
复制
@Injectable()
export class ExampleAuth implements HttpInterceptor {

  intercept(request: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {

    console.log('Checking Interceptor'); // This is never called
    return next.handle(request);
  }

}

home.componet

代码语言:javascript
复制
  @Component({
  selector: 'home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
   })

   export class LoginComponent implements OnInit{
   constructor(private someservice: SomeotherService,
          private _router: Router) {

    ngOnInit() {
    window.location.href ="https://GoingforAuthentication.com"
     }


   }


  }

app.module.ts

代码语言:javascript
复制
@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpModule
  ],
  providers: [
    SomeotherService,
    ExampleAuth
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-26 09:38:02

我不认为你需要用拦截器。此逻辑将特定于您的组件。您可以订阅ActivatedRoute中的可观测值来动态获取查询参数。

代码语言:javascript
复制
import { ActivatedRoute } from '@angular/router';
...

  constructor(
    private route: ActivatedRoute
  ) { }

  ngOnInit() {
    this.route.queryParams.subscribe(queryParam => {
      console.log(queryParam.someValue);
      // Do something with new query param here. Eg. update view, api call, etc.
    });
  }

这样,即使在内部更新角度代码中的查询参数,也可以在订阅查询参数时相应地更新视图。

票数 0
EN

Stack Overflow用户

发布于 2019-06-26 07:57:45

providers的变化

代码语言:javascript
复制
ExampleAuth

至:

代码语言:javascript
复制
{ provide: HTTP_INTERCEPTORS, useClass: ExampleAuth, multi: true }

还请检查以下内容:https://angular.io/guide/http#intercepting-requests-and-responses

票数 0
EN

Stack Overflow用户

发布于 2019-06-26 08:20:47

Http拦截器用来通过HttpClient的调用拦截您的请求。当你的应用程序启动时,你所需要的只是检查你是否需要在url中使用params。为此,您需要在您的ActivatedRoute中注入app.component.ts服务。

代码语言:javascript
复制
constructor(private _route: ActivatedRoute, ....) { ... }

并在someValue中检查ngOnInit

代码语言:javascript
复制
ngOnInit() {
    const someValue = this._route.snapshot.queryParams['someValue'];
    if (someValue == '....') {
        // have been redirected from auth
    }
}

https://angular.io/api/router/ActivatedRoute

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56767868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档