0

我从 index.html 中的 url 中删除了查询参数,但我希望在 app.component.html 中使用这些查询参数,并且我的 app.component.ts 使用 ActivatedRoute,现在当我在 index.html 中有这个脚本时,然后我的应用程序。 componen.ts 也没有收到那些 queru 参数,我怎样才能让 app.component.ts 有我的查询参数?

这是我在 index.html 中删除查询参数的脚本:

    <script type="text/javascript">
    var url = window.location.toString();
   if(url.indexOf("?") > 0) {
      var sanitizedUrl = url.substring(0, url.indexOf("?"));
      window.history.replaceState({}, document.title, sanitizedUrl);
    }
  </script>

这是我的 app.component.ts 来解析查询参数:

import {ApiService} from './api.service';
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Converter} from './converter';
import {Title} from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  username = '';
  department = '';

   json;
  constructor(
    private feedbackService: ApiService,
    private titleService: Title,
     private route: ActivatedRoute
    ) {
  }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      if (params.hasOwnProperty('username')) {
        this.username = params['username'];
      }     
      if (params.hasOwnProperty('department')) {
        this.department = params['department'];
      }     
    });
  }
4

2 回答 2

3

你能不能把这些查询参数放到会话存储中,然后访问 app.component.ts 中的会话存储?

  <script type="text/javascript">
    var url = window.location.toString();
   if(url.indexOf("?") > 0) {
      var [ sanitizedUrl, queryParams ] = url.split('?')
      window.history.replaceState({}, document.title, sanitizedUrl);
      sessionStorage.setItem('params', queryParams)
    }
  </script>
//app.component.ts

export class AppComponent implements OnInit {
  username = '';
  department = '';

   json;
  constructor(
    private feedbackService: ApiService,
    private titleService: Title,
     private route: ActivatedRoute
    ) {
  }

  ngOnInit() {
    let params = sessionStorage.getItem('params');
    // parse params logic here // 

    if (params.hasOwnProperty('username')) {
        this.username = params['username'];
      }     
      if (params.hasOwnProperty('department')) {
        this.department = params['department'];
      }     

    };
  }

于 2019-06-16T19:24:33.087 回答
1

经过一些研究,没有明确的解决方案,但我确实设法创建了一些应该可行的解决方案。

为了使其正常工作,其背后的想法是保持查询参数正常运行。

创建一个保存数据的服务:

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SampleService {
  data: BehaviorSubject<any> = new BehaviorSubject({});
  data$: Observable<any> = this.data.asObservable();
}

之后,在你的app.component.ts

constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private sampleService: SampleService
  ) {}

  ngOnInit(): void {
    this.sampleService.data$.subscribe(console.log);
    this.activatedRoute.queryParams.subscribe((data: {username: string, departure: string}) => {
      if(data.hasOwnProperty('username') || data.hasOwnProperty('departure')) {
        this.sampleService.data.next(data);
      }
      setTimeout(() => { // note this timeout is mandatory and it makes this kinda cheatfix the whole thing
        this.router.navigateByUrl('/');
      });
    });
  }

这样,您的参数将保存在 SampleService 的数据行为主题中。如果你想在某个地方使用它,你所要做的就是注入服务,然后订阅data$.

请注意,此解决方案包含一些应谨慎处理的订阅,但不是此问题的主题。

可以在这里找到简短的演示。当您使用 queryParams 作为 /?username=foo 打开 stackblitz 应用程序时检查演示控制台。

于 2019-06-16T19:45:37.080 回答