<address id="tl19n"></address>
      <address id="tl19n"><nobr id="tl19n"></nobr></address>

        <address id="tl19n"></address>

          <address id="tl19n"></address>
          <form id="tl19n"></form>

              首頁 > QQ技巧 > 從 Angular 中的 URL 獲取查詢參數

              從 Angular 中的 URL 獲取查詢參數

              時間:2021-04-23 20:46 作者:QQ地帶 我要評論

              通過注入ActivatedRoute的實例,可以訂閱各種可觀察對象,包括queryParams和params observable。以下是范例:
               
              import { ActivatedRoute } from '@angular/router';  // 用于獲取路由參數
              import { Component, OnInit } from '@angular/core';
              import { DomSanitizer } from '@angular/platform-browser'; // 用于HTML過濾
              import { Location } from '@angular/common'; // 用于回退瀏覽記錄
               
              import { NewsDetailService } from '../news-detail.service';
               
              @Component({
                selector: 'app-news-detail',
                templateUrl: './news-detail.component.html',
                styleUrls: ['./news-detail.component.css']
              })
              export class NewsDetailComponent implements OnInit {
               
                newsDetailData = null;
                newsUrl = null;
               
                constructor(private newsDetailService: NewsDetailService,
                  private domSanitizer: DomSanitizer,
                  private route: ActivatedRoute,
                  private location: Location) { }
               
                ngOnInit() {
                  this.showNewsDetailData();
                }
               
                // 展示新聞詳情數據
                showNewsDetailData() {
                  this.route.queryParams.subscribe(p => {
                    this.newsUrl = p.newsUrl // 獲取參數
               
                    this.newsDetailService.getNewsData(this.newsUrl).subscribe(
                      (newsApiData) => this.newsDetailData =
                        this.domSanitizer.bypassSecurityTrustHtml(newsApiData.toString()) //HTML過濾
                    );
               
                  });
                }
               
                // 返回
                goback() {
                  // 瀏覽器回退瀏覽記錄
                  this.location.back();
                }
              }

              標簽: Angular
              頂一下
              (0)
              0%
              踩一下
              (0)
              0%

              Google提供的廣告

              国产古代一级a毛片,国产成人午夜在线直播,write.as 当众老师
                <address id="tl19n"></address>
                  <address id="tl19n"><nobr id="tl19n"></nobr></address>

                    <address id="tl19n"></address>

                      <address id="tl19n"></address>
                      <form id="tl19n"></form>