vue修改网站标题,关键词,描述--js文件引用版

代码 

这两种写法效果是一样的,不过加载页面的时候,title会先显示插值表达式{{Seo.SeoTitle}},在显示数据。

推荐:第二种写法 v-text="Seo.SeoTitle"

<title>{{Seo.SeoTitle}}</title>
<title v-text="Seo.SeoTitle"></title>
 <!DOCTYPE html>
<html>

<head id="head">
	<meta charset="utf-8"> 
    <title v-text="Seo.SeoTitle"></title>
	<script src="js/vue.min.js"></script>
	<meta name="keywords" :content="Seo.SeoKeywords">
	<meta name="description" :content="Seo.SeoDescription">
</head>

<body>
	<h1>vue修改网站标题,关键词,描述</h1>
	 
	<script>
		var head = new Vue({
			el: '#head',
			data: {
				Seo: {
					SeoTitle: '',
					SeoKeywords: '',
					SeoDescription: ''
				},
			},
			mounted() {
				this.getSEO();
			},
			methods: {
				getSEO() {
					let that = this;
					that.Seo.SeoTitle = "SeoTitle网站标题";
					that.Seo.SeoKeywords = "SeoKeywords关键词";
					that.Seo.SeoDescription = "SeoDescription描述";
				},
			},
		});
	</script>

</body>

</html>

结果


axios请求

<head id="head">
	<meta charset="utf-8">
	<title v-text="Seo.SeoTitle"></title>
	<meta name="keywords" :content="Seo.SeoKeywords">
	<meta name="description" :content="Seo.SeoDescription"> 
</head>
<!-- seo -->
<script>
    var head = new Vue({
        el: "#head",
        data: {
            //seo
            Seo:{ SeoTitle: '', SeoKeywords: '', SeoDescription: ''},
        },
        mounted() {
            //seo
            this.getSEO();
        },
        methods: {
            //seo、、、、、、、、、、、、、、
            getSEO() { 
                let that = this;
                axios.get(CmsAPI + '/zixun/getWebsite', {
                    params: {
                        SiteName: 'quanxi' 
                    }
                }).then(res => { 
                    let seoData=res.data.data[0];  
                    that.Seo.SeoTitle = seoData.SeoTitle==null?seoData.name:seoData.SeoTitle;
                    that.Seo.SeoKeywords = seoData.SeoKeywords==null?seoData.name:seoData.SeoKeywords;
                    that.Seo.SeoDescription = seoData.SeoDescription==null?seoData.name:seoData.SeoDescription;
                }).catch(function(error) { // 请求失败处理  
                    console.error('SEO-error');
                    console.error(error);
                });
            },
        },
    });
</script>
<!-- seo end -->

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-极纪元JJY.Cheng

客官,1分钱也是爱,给个赏钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值