在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全局动态添加meta属性(title,keywords,description)”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
编程基础网
本文标题为:vue全局动态添加meta属性(title,keywords,description)
基础教程推荐
猜你喜欢
- vue项目中解决 IOS + H5 滑动边界橡皮筋弹性效果(解决思路) 2024-01-16
- 微信小程序使用webview打开pdf文档以及显示网页内 2022-08-30
- Linux 之 HTML 页面转图片软件:wkhtmltox 2023-10-25
- 魔兽世界火法神器故事全面介绍_wow火法神器获得方法推荐 2024-01-07
- layui数据表格搜索 2022-12-16
- CSS实现HTML元素透明的方法小结 2024-02-07
- ajax回调打开新窗体防止浏览器拦截有效方法 2022-12-28
- ajax实现文件异步上传并回显文件相关信息功能示例 2023-02-14
- 使用CSS和Java来构建管理仪表盘布局的实例代码 2024-01-07
- JavaScript原始值与包装对象的详细介绍 2023-12-26
