在Vue中动态地添加meta标签的keywords属性和description属性可以通过修改页面的头部信息来实现。下面小编给大家简单介绍一下具体操作代码!
vue全局动态添加meta属性代码
//打开main.js,在router.beforeEach里新增以下代码
router.beforeEach((to,from,next)=>{
if(Object.keys(to.meta).length>0 && to.matched.length>0){
let this_meta=to.matched[to.matched.length-1].meta
//动态添加标题
if(this_meta.title)document.title=this_meta.title
let head = document.getElementsByTagName('head');
//动态添加keywords
let keyword = document.createElement('meta');
if(document.querySelector('meta[name="keywords"]')){
document.querySelector('meta[name="keywords"]').setAttribute('content',this_meta.keywords)
}else{
keyword.setAttribute('name','keywords')
keyword.setAttribute('content',this_meta.keywords)
head[0].appendChild(keyword)
}
//动态添加description
let description = document.createElement('meta');
if(document.querySelector('meta[name="description"]')){
document.querySelector('meta[name="description"]').setAttribute('content',this_meta.description)
}else{
description.setAttribute('name','description')
description.setAttribute('content',this_meta.description)
head[0].appendChild(description)
}
}
next()
})
vue语法中$router和$route有什么不同?附示例代码
上面是“vue全局动态添加meta属性(title,keywords,description)”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_14404.html
workflows工作流
荷塘月色ComfyUI工作流
一轮月亮悬挂在树上的天空中ComfyUI工作流
一种长着彩虹翅膀的虫子comfyui工作流
汗血宝马ComfyUI工作流
一只可爱的小鸟在飞翔ComfyUI工作流
一个戴着破旧莎帽子的机械忍者站在冰林中
一个迷人而丰满的女巫和一只黑猫骑着飞天扫帚
葡萄庄园白葡萄酒ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

js实现下雪特效
利用CSS3做一个星级评分样式
利用canvas画几个好玩的星云物种
利用css绘画棋盘布局(象棋)
canvas黑洞漩涡(canvas+js)
canvas空间文本射线
一个包含老黄历、佛历、道历、星宿等数据的日历网站
js+css3做一个灯泡开灯关灯效果











