问题描述
在我的网络应用程序中,有静态页面和动态页面。出于 SEO 目的,我必须实现此链接 https://www.talkingdotnet.com/how-to-set-canonical-url-in-angular-7/ 中提到的规范标签。我为规范标签创建了一个共享服务,并在 appcomponent 的 ngoninit 中调用.
应用组件:
import { Component } from '@angular/core';
import { HostListener } from '@angular/core';
import { SharedService } from './core/services/shared.service';
import { MetaService } from './core/canonical/canonical';
@Component({
selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor( public sharedService:SharedService,private MetaService: MetaService){
}
ngOnInit() {
this.MetaService.createCanonicalURL();
}
title = 'experience-travel-ui';
//this will reset the id while navigating back
@HostListener('window:popstate',['$event'])
onPopState(event) {
this.sharedService.changeSelectedId(null);
}
}
共享规范服务:
import { Injectable,Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Injectable({
providedIn: 'root'
})
export class MetaService {
constructor(@Inject(DOCUMENT) private dom) { }
createCanonicalURL() {
let link: HTMLLinkElement = this.dom.createElement('link');
link.setAttribute('rel','canonical');
this.dom.head.appendChild(link);
console.log(this.dom,"KKKKKK")
link.setAttribute('href',this.dom.URL);
}
}
真正的问题是规范网址仅针对静态网址更新,但对于动态网址却不起作用。令人惊讶的是,页面刷新后动态 url 规范 url 正在更新。我该如何解决它,并且必须找出与其他相同结构化 url 的父子关系。有人可以解决这个问题吗。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)