Как создать оптимизированные для SEO URL-адреса с категорией / подкатегориями / слаг статьи в проекте angular? - PullRequest
0 голосов
/ 06 февраля 2020

Я использую Angular 8 версию для создания приложения новостей. Мне нужно отобразить ссылку так: www.domain.com/category/category/title и www.domain.com/category. Как мне этого добиться?

Спасибо

1 Ответ

2 голосов
/ 06 февраля 2020

Вы буквально подразумеваете ссылку с текстом ссылки, который отличается от ее href, вот так ?

Если так,

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

Машинопись:

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

Или вы хотите сделать что-то еще с URL самой страницы?

РЕДАКТИРОВАТЬ:

Модуль маршрутизации

// 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 }

Компонент категории

// 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
    });
  }
}
...