LoginSignup
0
0

More than 3 years have passed since last update.

Vue.jsでカスタムなページタイトルを設定する

Last updated at Posted at 2020-11-14

はじめに

ちょっとの間悩んでた表題の問題が他人のコード見てたらふと解決したので軽くまとめていきます。Vue.jsでページタイトルをデフォルトのアプリ名から変更する方法です。

手順

まず、Vue.jsでページタイトルを規定してるコードはpublic/index.html内にあります。

public/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title> <!--ここですここ、ここ-->
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

しかし、ここは変数で指定してありますしあまりこのファイルを弄るのはよくないようです。なのでどこでこの変数を指定するのか書いていきます。
まず、rootフォルダにvue.config.jsというJavaScriptファイルを作ります。
そしてそこに次のように入力してください。

vue.config.js
module.exports = {
    pages: {
        index: {
            entry: 'src/main.js', // ここは変えないで
            title: 'ページタイトル', // 好きな文字列をいれてください
        }
    }
}

そうしてビルドし直すと無事ページタイトルが変更されてると思います。

最後に

サイト自体のタイトルの後にページのタイトルをいれる方法はそのうち実装しようと思うので成功したら追記します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0