我怎样才能重新渲染这个角度分量?

发布于 2025-01-17 06:33:52 字数 1955 浏览 0 评论 0原文

我有两个组件。标题和搜索表单。标题为您提供了一个用于搜索食谱的表单。当您提交表单时,您将被重定向到显示搜索结果的搜索页面。此功能有效。但是,当我在搜索页面上时,我想提交另一个搜索(从标题)。搜索页面不会重新呈现和更新以显示新的搜索结果。

header.component.ts

export class HeaderComponent {

  constructor(private router: Router) { }

  query!: any;

  startSearch() {
   this.query = ((document.getElementById('form-control me-2') as 
      HTMLInputElement).value);
   console.log("Query:", this.query);
   this.router.navigate(['/search', this.query])
  }
}

header.component.ts

<div class="nav-overlay uk-navbar-left uk-flex-1" hidden>
        <div class="uk-navbar-item uk-width-expand">
            <form class="uk-search uk-search-large uk-width-1-1" (ngSubmit)="startSearch();" >
                <input id="form-control me-2" class="uk-search-input" type="search" placeholder="Search..." autofocus>
            </form>
        </div>
        <a class="uk-navbar-toggle" uk-close="ratio:2" uk-toggle="target: .nav-overlay; animation: uk-animation-fade" href="#"></a>
    </div>

search.component.ts

export class SearchComponent implements OnInit, OnDestroy{
  searchSub!: Subscription;
  errorMessage: string = '';
  
    constructor(
        private searchService: SearchService,
        private route: ActivatedRoute
    ) { }

    results!: ISearchResults;
    query!: any;

    ngOnInit(): void {
        this.route.paramMap.subscribe(params => {
            console.log(params);
            this.query = params.get('query');
        });
        this.searchSub = this.searchService
            .search(this.query)
            .subscribe({
                next: results => {
                    this.results = results,
                        console.log("Search query : ", this.query);
                }
            }
        );
    }

    ngOnDestroy() {
        this.searchSub.unsubscribe();
    }
}

I have two components. A header and a search form. The header gives you a form to search for a recipe. When you submit the form, you are redirected to the search page that shows you your search results. This functionality works. However, while I'm on the search page I want to submit another search(from the header). The search page will not re-render and update to show the new search results.

header.component.ts

export class HeaderComponent {

  constructor(private router: Router) { }

  query!: any;

  startSearch() {
   this.query = ((document.getElementById('form-control me-2') as 
      HTMLInputElement).value);
   console.log("Query:", this.query);
   this.router.navigate(['/search', this.query])
  }
}

header.component.ts

<div class="nav-overlay uk-navbar-left uk-flex-1" hidden>
        <div class="uk-navbar-item uk-width-expand">
            <form class="uk-search uk-search-large uk-width-1-1" (ngSubmit)="startSearch();" >
                <input id="form-control me-2" class="uk-search-input" type="search" placeholder="Search..." autofocus>
            </form>
        </div>
        <a class="uk-navbar-toggle" uk-close="ratio:2" uk-toggle="target: .nav-overlay; animation: uk-animation-fade" href="#"></a>
    </div>

search.component.ts

export class SearchComponent implements OnInit, OnDestroy{
  searchSub!: Subscription;
  errorMessage: string = '';
  
    constructor(
        private searchService: SearchService,
        private route: ActivatedRoute
    ) { }

    results!: ISearchResults;
    query!: any;

    ngOnInit(): void {
        this.route.paramMap.subscribe(params => {
            console.log(params);
            this.query = params.get('query');
        });
        this.searchSub = this.searchService
            .search(this.query)
            .subscribe({
                next: results => {
                    this.results = results,
                        console.log("Search query : ", this.query);
                }
            }
        );
    }

    ngOnDestroy() {
        this.searchSub.unsubscribe();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-24 06:33:52

此处对此进行了解答。基本上 ngOnInit() 没有被调用,因为组件已经初始化。
将搜索放入 paramMap 订阅中,然后当路由参数更改时,您的结果就会更新。

ngOnInit(): void {
    this.route.paramMap.subscribe(params => {
        console.log(params);
        this.query = params.get('query');
        this.searchService
        .search(this.query)
        .subscribe({
            next: results => {
                this.results = results,
                    console.log("Search query : ", this.query);
            }
        });
    
    });
    
}

This is answered here. Basically ngOnInit() is not being called because the component is already initialized.
Put the search inside you paramMap subscription, then your results get updated when the route params change.

ngOnInit(): void {
    this.route.paramMap.subscribe(params => {
        console.log(params);
        this.query = params.get('query');
        this.searchService
        .search(this.query)
        .subscribe({
            next: results => {
                this.results = results,
                    console.log("Search query : ", this.query);
            }
        });
    
    });
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文