{"id":1305,"date":"2023-03-18T10:37:18","date_gmt":"2023-03-18T02:37:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1305"},"modified":"2023-04-29T09:16:04","modified_gmt":"2023-04-29T01:16:04","slug":"vue-cli-project-set-favicon-and-dynamically-modify-favicon","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/vue-cli-project-set-favicon-and-dynamically-modify-favicon\/","title":{"rendered":"vue-cli\u9879\u76ee\u8bbe\u7f6efavicon\u4ee5\u53ca\u52a8\u6001\u4fee\u6539favicon"},"content":{"rendered":"<h2>\u9759\u6001\u8bbe\u7f6e<\/h2>\n<p>\u52a8\u6001\u66f4\u65b0\u4e4b\u524d\u9700\u8981\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684favicon\u3002<\/p>\n<p>\u4f7f\u7528vue-cli\u642d\u5efa\u7684vue\u9879\u76ee\u91cc\u9762\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2astatic\u76ee\u5f55\uff0c\u5b58\u653e\u9759\u6001\u6587\u4ef6\u3002favicon\u56fe\u7247\u653e\u5230\u8be5\u76ee\u5f55\u4e0b\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u7136\u540e\u518dindex.html\u4e2d\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;link rel=&quot;shortcut icon&quot; type=&quot;image\/x-icon&quot; href=&quot;static\/favicon.ico&quot;&gt;<\/code><\/pre>\n<p>\u7136\u540e\u5237\u65b0\u6d4f\u89c8\u5668\uff0c\u5c31\u4f1a\u66f4\u65b0\u3002<\/p>\n<h2>html-webpack-plugin\u8bbe\u7f6e<\/h2>\n<p>vue-cli\u811a\u624b\u67b6\u9ed8\u8ba4\u5b89\u88c5\u4e86html-webpack-plugin\uff0c\u4fee\u6539build\u76ee\u5f55\u4e0b\uff1a<\/p>\n<p>build\/webpack.dev.conf.js<\/p>\n<pre><code class=\"language-javascript\">\/\/ https:\/\/github.com\/ampedandwired\/html-webpack-plugin\nnew HtmlWebpackPlugin({\n  filename: &#039;index.html&#039;,\n  template: &#039;index.html&#039;,\n  inject: true,\n  favicon: path.resolve(&#039;.\/static\/favicon.ico&#039;) \/\/ \u914d\u7f6efavicon.ico\n})<\/code><\/pre>\n<p>build\/webpack.prod.conf.js<\/p>\n<pre><code class=\"language-javascript\">\/\/ see https:\/\/github.com\/ampedandwired\/html-webpack-plugin\nnew HtmlWebpackPlugin({\n  filename: process.env.NODE_ENV === &#039;testing&#039;\n    ? &#039;index.html&#039;\n    : config.build.index,\n  template: &#039;index.html&#039;,\n  inject: true,\n  favicon: path.resolve(&#039;.\/static\/favicon.ico&#039;), \/\/ \u914d\u7f6efavicon.ico\n  minify: {\n    removeComments: true,\n    collapseWhitespace: true,\n    removeAttributeQuotes: true\n    \/\/ more options:\n    \/\/ https:\/\/github.com\/kangax\/html-minifier#options-quick-reference\n  },\n  \/\/ necessary to consistently work with multiple chunks via CommonsChunkPlugin\n  chunksSortMode: &#039;dependency&#039;\n}),<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u914d\u7f6efavicon\u56fe\u6807\u7684\u8def\u5f84\u662f\u76f8\u5bf9\u8def\u5f84<\/p>\n<\/blockquote>\n<h2>\u52a8\u6001\u8bbe\u7f6e<\/h2>\n<p>\u5982\u4f55\u4ece\u670d\u52a1\u5668\u52a8\u6001\u83b7\u53d6\u56fe\u7247\u5462\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u50cf\u4e0a\u4f20\u6587\u4ef6\u4e00\u6837\uff0c\u968f\u610f\u66f4\u6362favicon\u3002<\/p>\n<p>\u65b9\u6cd5\uff1a\u52a8\u6001\u521b\u5efalink\u6807\u7b7e\uff0c\u7136\u540e\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/260857\/changing-website-favicon-dynamically\">https:\/\/stackoverflow.com\/questions\/260857\/changing-website-favicon-dynamically<\/a><\/p>\n<pre><code class=\"language-javascript\">(function() {\n    var link = document.querySelector(&quot;link[rel*=&#039;icon&#039;]&quot;) || document.createElement(&#039;link&#039;);\n    link.type = &#039;image\/x-icon&#039;;\n    link.rel = &#039;shortcut icon&#039;;\n    link.href = &#039;http:\/\/www.stackoverflow.com\/favicon.ico&#039;;\n    document.getElementsByTagName(&#039;head&#039;)[0].appendChild(link);\n})();<\/code><\/pre>\n<p>\u9644\uff1a\u4f7f\u7528axios\u4e0a\u4f20\u56fe\u7247<\/p>\n<pre><code class=\"language-javascript\">uploadFavicon (val) {\n  let that = this;  \n  let Fr = new FileReader;\n  let file = val.target.files[0];\n  \/\/\u83b7\u53d6\u9700\u8981\u66f4\u6362\u7684img\u6807\u7b7e\u7684id\n  let img = document.getElementById(val.srcElement.name.split(&#039;|&#039;)[0]);\n  Fr.readAsDataURL(file);\n  Fr.onloadend = function () {\n    img.src = this.result;\n  };\n  let fd = new FormData();\n  \/\/addend(&#039;\u53c2\u6570\u540d&#039;, &#039;\u53c2\u6570\u503c&#039;)\uff0c\u53c2\u6570\u540d\u9700\u8981\u548c\u540e\u7aef\u5bf9\u5e94\n  fd.append(&#039;InputFile&#039;, file);\n  fd.append(&#039;logo_id&#039;, val.srcElement.name.split(&#039;|&#039;)[1]);\n  \/\/vue\u9879\u76ee\u4e2d\u4e3a\u4e86\u65b9\u4fbf\u66f4\u6539\u4e00\u4e0baxios\u539f\u578b\u94fe\uff0c\u5176\u5b9e\u5c31\u662f\u53d1\u9001\u4e00\u4e2aaxios\u8bf7\u6c42\n  that.Axios.post(that.prefix + &#039;\/appblog\/logo_update\/&#039;, fd, new Blob([fd], { type: &#039;multipart\/form-data&#039; }))\n    .then(function (res) {\n      if (res.data.status == 1) {\n        util.notification(&#039;success&#039;, &#039;\u6210\u529f&#039;, res.data.success_msg);\n      } else {\n        util.notification(&#039;error&#039;, &#039;\u5931\u8d25&#039;, res.data.error_msg);\n      }\n      img.value = &#039;&#039;;\n    })\n    .catch(function (err) {\n      console.log(err);\n    });\n  \/\/\u4e0a\u4f20\u4e4b\u540e\u4fee\u6539\u4e86\u4e00\u4e0baxios\u7684\u539f\u578b\u94fe\uff0c\u56e0\u4e3a\u5168\u5c40\u5176\u4ed6\u9875\u9762\u90fd\u9700\u8981\n  that.Axios.defaults.headers.post[&#039;Content-Type&#039;] = &#039;application\/x-www-form-urlencoded&#039;;\n  that.Axios.defaults.transformRequest  = function (data) {\n    let ret = &#039;&#039;;\n    for (let it in data) {\n      ret += encodeURIComponent(it) + &#039;=&#039; + encodeURIComponent(data[it]) + &#039;&amp;&#039;\n    }\n    return ret.slice(0, ret.length - 1);\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9759\u6001\u8bbe\u7f6e \u52a8\u6001\u66f4\u65b0\u4e4b\u524d\u9700\u8981\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684favicon\u3002 \u4f7f\u7528vue-cli\u642d\u5efa\u7684vue\u9879\u76ee\u91cc\u9762\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2ast [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1305","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1305"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1305\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}