我需要在index.html页面中使用vue js变量。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="shortcut icon" :href="$store.state.siteLogo" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="static/layui/css/layui.css" />
<link rel="stylesheet" type="text/css" href="static/layui/common.css" />
<title id="siteTitle">{{$store.state.siteTitle}}</title>
</head>
<body>
<div id="app"></div>
</body>
</html>在那里,对于标题,我需要从服务中获取一个值。该值能够从服务中获取,并将其分配给存储js变量。我的尝试和上面的一样,但它不工作,因为它是一个html文件。有没有人知道我怎样才能做到这一点?它是这样工作的。在App.vue创建的方法中,如果我们如下所示设置document.title。
created() {
document.title = this.$store.state.siteTitle;
}但现在我需要知道如何在服务中更改快捷方式图标。有人知道吗?
发布于 2020-11-26 10:08:09
很简单,你可以做到这一点。你可以从你的服务或你的vuex状态中动态地改变收藏夹图标。但是,如果您不想使用这些vanilajs的东西,那么vue-meta.nuxtjs.org是为您准备的。
created () {
var link =
document.querySelector("link[rel*='icon']") ||
document.createElement("link");
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href =
"https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196";
document.getElementsByTagName("head")[0].appendChild(link);
}或者,您可以将其放入一个方法中,以便在监视或单击事件期间动态使用它。
created() {
this.initIcon();
},
methods: {
initIcon() {
var link =
document.querySelector("link[rel*='icon']") ||
document.createElement("link");
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href = this.icon;
document.getElementsByTagName("head")[0].appendChild(link);
},
click() {
this.icon = '' // new updated icon here
this.initIcon();
},
},https://stackoverflow.com/questions/65000760
复制相似问题