首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带参数的Angular routerLink正在更改网址,但不会导航或重新加载

带参数的Angular routerLink正在更改网址,但不会导航或重新加载
EN

Stack Overflow用户
提问于 2021-11-15 06:27:24
回答 1查看 95关注 0票数 0

它会更改url,但不会重新加载。

我基本上是试图重新加载页面,但有一个不同的数据。

post.component.ts

代码语言:javascript
复制
constructor(private postService :PostService, private activateRoute: ActivatedRoute)
  
    {}

private getPostsbyCategory(){
          this.postService.getPostsByCategory(this.catid).subscribe((data:PostModel[]) => {
            this.postbycategory = data;
            console.log(data)
          }, error => {
            throwError(error);}
            // 
          );}


 ngOnInit() {

    this.id = this.activateRoute.snapshot.params.id;
    this.catid = this.activateRoute.snapshot.params.catid;

    console.log(this.id);
    console.log(this.catid)
    this.getposts();
    this.getPostsbyCategory();

post.component.html

代码语言:javascript
复制
 <a routerLink="/view-post/{{post.catid}}/{{post.id}}" class="card">

下面是路由文件app-routing.module.ts

代码语言:javascript
复制
 import { NgModule,Component } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { ReactiveFormsModule } from '@angular/forms';
    import { FormsModule } from '@angular/forms';
    import { TestComponent } from './test/test.component';
    
    import { HomeComponent } from './home/home.component';


    import { ViewPostComponent } from './component/post/view-post/view-post.component';
    import { ViewCategoryComponent } from './component/category/view-category/view-category.component';
    const routes: Routes = [
    
      { path: '', component: HomeComponent },
      {path: 'home', component: HomeComponent },
      {path: 'test', component: TestComponent },
      {path: 'topics', component: ListTopicsComponent },
      { path: 'view-topics/:id', component: ViewTopicsComponent },
      {path : 'view-post/:catid/:id', component:ViewPostComponent}, 
    
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes , {onSameUrlNavigation: 'reload'})],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }

它改变了网址,但没有重新加载,我基本上是试图重新加载页面,但有一个不同的数据。

EN

回答 1

Stack Overflow用户

发布于 2021-11-15 07:06:37

尝试将链接放在一组方括号中,并将链接本身放在单引号中。

代码语言:javascript
复制
 <a routerLink="['/view-post/{{post.catid}}/{{post.id}}']" class="card">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69970186

复制
相关文章

相似问题

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