-1

I am using Angular 8 Version to create a news app. I need to display the link like this: www.domain.com/category/category/title and www.domain.com/category. How can I achieve this?

Thanks

1 Answers1

5

Do you literally just mean a link with link text that differs from its href, like this?

If so,

<a [routerLink]="url">{{seoUrl}}</a>

Typescript:

url: 'https://www.google.com';
seoUrl: 'https://www.google.com/category/slug';

Or do you want to do something more with the url of the page itself?

EDIT:

Routing module

// Declare two routes with an optional title. We will always redirect to the title route. Order matters here - routes will be matched in order.
{ path: 'category/:category/:title', component: CategoryComponent },
// this path declares a route similar to /category/abc where abc is a category 
{ path: 'category/:category', component: CategoryComponent }

Category component

// component decorator omitted for brevity
export class CategoryComponent implements OnInit {
  constructor(private route: ActivatedRoute,
    private router: Router
  ) {
  }

  ngOnInit(): void {
    // get the category from the url
    const categoryName = this.route.snapshot.paramMap.get('category');
    const titleName = this.route.snapshot.paramMap.get('title');

    // TODO: implement categoryService
    this.categoryService.getCategory(categoryName).subscribe(category => {
      if (!title) {
        this.router.navigateByUrl(`/category/${category.name}/${category.title}`);
        return;
      }

      // TODO: render the category
    });
  }
}
Kurt Hamilton
  • 12,490
  • 1
  • 24
  • 40
  • how can we use routing for details component. My all news are going to display in same details page. – Mohandas P G Feb 06 '20 at 14:47
  • 1
    I'll edit my answer. This needs several more bits of code. – Kurt Hamilton Feb 06 '20 at 14:47
  • 1
    I've added some routing to do what I think you want - it extracts the category and title from the url, and redirects to the title if it isn't there. I've not added in any null checking, which you would definitely want for user input (which the url is). – Kurt Hamilton Feb 06 '20 at 15:04
  • 1
    @KurtHamilton Your routing module setup will make Angular re-render the component. According to this answer [https://stackoverflow.com/a/49159166/16422535](https://stackoverflow.com/a/49159166/16422535), the redirectTo function should be used for the path without title. – Khoa Le May 09 '22 at 07:46
  • @KhoaLe that is a correct observation. My answer includes an async call to a service in order to determine the redirect URL, so the redirect can't be hard-coded into the routing config. In the situation where the redirect URLs are known at design time you would use `redirectTo` as you correctly observed. The question wasn't specific on how the redirect URL should be built - I constructed the answer for one use case. – Kurt Hamilton May 09 '22 at 09:12
  • What about sanitizing the slug to be a safeUrl? – Shadi Alnamrouti Jul 17 '22 at 17:48
  • @ShadiAlnamrouti that would of course be necessary if the values were unsafe for URLs. That's a different question though. – Kurt Hamilton Jul 19 '22 at 11:45