How can I bind the html content in vuejs?</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2016-04-14T02:33:05.857" class="fromnow">Apr 14 '16 at 02:33</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2022-12-10T13:12:28.363" datetime="2022-12-10T13:12:28.363">Dec 10 '22 at 13:12</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 118,907 times"> <span class="fc-light mr2">Viewed</span> 1.2e+01k times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="36612847" data-ownerid="2544762" data-score="80"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="36612847"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="80">80</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value="0">0</div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I'm trying a demo on vuejs. Now I want the html title to bind a vm field.</p> <p>The below is what I tried:</p> <p><code>index.html</code></p> <pre><code><!DOCTYPE html> <html id="html"> <head> <title>{{ hello }}</title> <script src="lib/requirejs/require.min.js" data-main="app"></script> </head> <body> {{ hello }} <input v-model="hello" title="hello" /> </body> </html> </code></pre> <p><code>app.js</code></p> <pre><code>define([ 'jquery', 'vue' ], function ($, Vue) { var vm = new Vue({ el: 'html', data: { hello: 'Hello world' } }); }); </code></pre> <p>But the title seemed not bounded, how to make it work?</p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/javascript" class="post-tag js-gps-track" title="show questions tagged 'javascript'" rel="tag">javascript</a> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/mvvm" class="post-tag js-gps-track" title="show questions tagged 'mvvm'" rel="tag">mvvm</a> <a href="../../questions/tagged/frontend" class="post-tag js-gps-track" title="show questions tagged 'frontend'" rel="tag">frontend</a> <a href="../../questions/tagged/vue.js" class="post-tag js-gps-track" title="show questions tagged 'vue.js'" rel="tag">vue.js</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Apr 14 '16 at 02:33">asked Apr 14 '16 at 02:33</time> <a href="../../users/2544762/alfred-huang" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2544762.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Alfred Huang" /> </a> <div class="s-user-card--info"> <a href="../../users/2544762/alfred-huang" class="s-user-card--link">Alfred Huang</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">17,654</li> <li class="s-award-bling s-award-bling__gold" title="32 gold badges">32</li> <li class="s-award-bling s-award-bling__silver" title="118 silver badges">118</li> <li class="s-award-bling s-award-bling__bronze" title="189 bronze badges">189</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-36612847" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="36612847" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-60822648" class="comment js-comment " data-comment-id="60822648" data-comment-owner-id="825568" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment60822648_36612847"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Try `el: document.documentElement`</span> – <a href="../../users/825568/joseph-silber" title="214,931 reputation" class="comment-user ">Joseph Silber</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment60822648_36612847"><span title="2016-04-14T02:43:46.430 License: CC BY-SA 3.0" class="relativetime-clean">Apr 14 '16 at 02:43</span></a></span> </div> </div> </li> <li id="comment-60822676" class="comment js-comment " data-comment-id="60822676" data-comment-owner-id="3720222" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment60822676_36612847"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I've always bound Vue to the body and then set `document.title` when I needed to update it. Conceptually this seems possible but I've never seen it this way.</span> – <a href="../../users/3720222/jeff" title="24,623 reputation" class="comment-user ">Jeff</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment60822676_36612847"><span title="2016-04-14T02:45:13.297 License: CC BY-SA 3.0" class="relativetime-clean">Apr 14 '16 at 02:45</span></a></span> </div> </div> </li> <li id="comment-100997265" class="comment js-comment " data-comment-id="100997265" data-comment-owner-id="1077102" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment100997265_36612847"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">in my case 'hello' is computed. i use v-bind:title="hello"</span> – <a href="../../users/1077102/oritk" title="554 reputation" class="comment-user ">OritK</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment100997265_36612847"><span title="2019-07-29T06:56:38.193 License: CC BY-SA 4.0" class="relativetime-clean">Jul 29 '19 at 06:56</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">9 Answers<span style="display:none;" itemprop="answerCount">9</span></h2> </div> </div> </div> <a name="40388120"></a> <div id="answer-40388120" class="answer accepted-answer" data-answerid="40388120" data-ownerid="906113" data-score="114" itemprop="acceptedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40388120"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="114">114</div> <div class="js-accepted-answer-indicator grid--cell fc-green-500 py6 mtn8"><div class="ta-center"><svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="m6 14 8 8L30 6v8L14 30l-8-8v-8z"></path></svg></div></div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>There are essentially two ways to solve it.</p> <h1>Use an existing Package</h1> <p>For example, <a class="external-link" href="https://vue-meta.nuxtjs.org/" rel="noreferrer"><strong>vue-meta</strong></a>:</p> <blockquote> <pre><code><template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App', metaInfo: { // if no subcomponents specify a metaInfo.title, this title will be used title: 'Default Title', // all titles will be injected into this template titleTemplate: '%s | My Awesome Webapp' } } </script> </code></pre> </blockquote> <h1>Create your own Component</h1> <p>Create a vue file containing:</p> <pre><code><script> export default { name: 'vue-title', props: ['title'], watch: { title: { immediate: true, handler() { document.title = this.title; } } }, render () { }, } </script> </code></pre> <p>Register the component using</p> <pre><code>import titleComponent from './title.component.vue'; Vue.component('vue-title', titleComponent); </code></pre> <p>Then you can use it in your templates, e.g.</p> <pre><code><vue-title title="Static Title"></vue-title> <vue-title :title="dynamic.something + ' - Static'"></vue-title> </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2020-12-03T14:25:36.850" class="relativetime">Dec 03 '20 at 14:25</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">str</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 02 '16 at 19:23">answered Nov 02 '16 at 19:23</time> <a href="../../users/906113/str" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/906113.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="str" /> </a> <div class="s-user-card--info"> <a href="../../users/906113/str" class="s-user-card--link">str</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">42,689</li> <li class="s-award-bling s-award-bling__gold" title="17 gold badges">17</li> <li class="s-award-bling s-award-bling__silver" title="109 silver badges">109</li> <li class="s-award-bling s-award-bling__bronze" title="127 bronze badges">127</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-40388120" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="40388120" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-71877796" class="comment js-comment " data-comment-id="71877796" data-comment-owner-id="2803743" data-comment-score="4"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">4</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment71877796_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">+1 I'd go with this since it's considered bad practise mounting your Vue instance to the html tag (instead of an isolated wrapper in body).</span> – <a href="../../users/2803743/kano" title="5,626 reputation" class="comment-user ">kano</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment71877796_40388120"><span title="2017-02-21T09:54:09.650 License: CC BY-SA 3.0" class="relativetime-clean">Feb 21 '17 at 09:54</span></a></span> </div> </div> </li> <li id="comment-72920106" class="comment js-comment " data-comment-id="72920106" data-comment-owner-id="2212909" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment72920106_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">In vue 2 it's `mounted` instead of `created`</span> – <a href="../../users/2212909/jared" title="767 reputation" class="comment-user ">Jared</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment72920106_40388120"><span title="2017-03-20T17:45:48.623 License: CC BY-SA 3.0" class="relativetime-clean">Mar 20 '17 at 17:45</span></a></span> </div> </div> </li> <li id="comment-72924730" class="comment js-comment " data-comment-id="72924730" data-comment-owner-id="906113" data-comment-score="4"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">4</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment72924730_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@Jared No, it's not. Both are actually valid [lifecycle hooks](https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks) but `created` is called before `mounted`.</span> – <a href="../../users/906113/str" title="42,689 reputation" class="comment-user ">str</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment72924730_40388120"><span title="2017-03-20T19:55:21.313 License: CC BY-SA 3.0" class="relativetime-clean">Mar 20 '17 at 19:55</span></a></span> </div> </div> </li> <li id="comment-72973556" class="comment js-comment " data-comment-id="72973556" data-comment-owner-id="2212909" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment72973556_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str is right, my bad. It was just that in my use case (slightly different than OP's) `created` didn't work out - I had to use `mounted` instead.</span> – <a href="../../users/2212909/jared" title="767 reputation" class="comment-user ">Jared</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment72973556_40388120"><span title="2017-03-21T21:15:41.800 License: CC BY-SA 3.0" class="relativetime-clean">Mar 21 '17 at 21:15</span></a></span> </div> </div> </li> <li id="comment-100742248" class="comment js-comment " data-comment-id="100742248" data-comment-owner-id="5552528" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment100742248_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">awesome! work for me! Bur I Have a small problem with description (dont work). I add meta description tag to my template for solving this.</span> – <a href="../../users/5552528/serhii-zelenchuk" title="175 reputation" class="comment-user ">Serhii Zelenchuk</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment100742248_40388120"><span title="2019-07-19T11:38:04.010 License: CC BY-SA 4.0" class="relativetime-clean">Jul 19 '19 at 11:38</span></a></span> </div> </div> </li> <li id="comment-104456294" class="comment js-comment " data-comment-id="104456294" data-comment-owner-id="5281051" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment104456294_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str Maybe you can help me. Look at this : https://stackoverflow.com/questions/59112611/how-can-i-solve-could-not-find-a-declaration-file-for-module-vue-headful</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment104456294_40388120"><span title="2019-11-30T02:14:13.377 License: CC BY-SA 4.0" class="relativetime-clean">Nov 30 '19 at 02:14</span></a></span> </div> </div> </li> <li id="comment-110930171" class="comment js-comment " data-comment-id="110930171" data-comment-owner-id="5281051" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110930171_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str The title content is change. But if I right-click on the website and select `View Page Source`, I see the title, description and keywords is don't change</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110930171_40388120"><span title="2020-07-04T13:15:41.927 License: CC BY-SA 4.0" class="relativetime-clean">Jul 04 '20 at 13:15</span></a></span> </div> </div> </li> <li id="comment-110930261" class="comment js-comment " data-comment-id="110930261" data-comment-owner-id="906113" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110930261_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@SuccessMan Yes that is how JavaScript DOM manipulations work. If that is not what you need, you have to change the meta data on the backend.</span> – <a href="../../users/906113/str" title="42,689 reputation" class="comment-user ">str</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110930261_40388120"><span title="2020-07-04T13:20:21.440 License: CC BY-SA 4.0" class="relativetime-clean">Jul 04 '20 at 13:20</span></a></span> </div> </div> </li> <li id="comment-110930320" class="comment js-comment " data-comment-id="110930320" data-comment-owner-id="5281051" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110930320_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str What do you mean "change the meta data on the backend"?</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110930320_40388120"><span title="2020-07-04T13:23:27.247 License: CC BY-SA 4.0" class="relativetime-clean">Jul 04 '20 at 13:23</span></a></span> </div> </div> </li> <li id="comment-110930697" class="comment js-comment " data-comment-id="110930697" data-comment-owner-id="906113" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110930697_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@SuccessMan The backend sends the source code to the browser and that is what "View Page Source" will show. JavaScript can only change the content *after* is has been sent to the browser.</span> – <a href="../../users/906113/str" title="42,689 reputation" class="comment-user ">str</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110930697_40388120"><span title="2020-07-04T13:43:30.193 License: CC BY-SA 4.0" class="relativetime-clean">Jul 04 '20 at 13:43</span></a></span> </div> </div> </li> <li id="comment-110942630" class="comment js-comment " data-comment-id="110942630" data-comment-owner-id="5281051" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110942630_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str Do you have a reference on how to do that?</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110942630_40388120"><span title="2020-07-05T02:38:55.373 License: CC BY-SA 4.0" class="relativetime-clean">Jul 05 '20 at 02:38</span></a></span> </div> </div> </li> <li id="comment-110945053" class="comment js-comment " data-comment-id="110945053" data-comment-owner-id="906113" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110945053_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@SuccessMan No because that highly depends on the technologies you are using and what exactly you want to do with it.</span> – <a href="../../users/906113/str" title="42,689 reputation" class="comment-user ">str</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110945053_40388120"><span title="2020-07-05T06:56:28.440 License: CC BY-SA 4.0" class="relativetime-clean">Jul 05 '20 at 06:56</span></a></span> </div> </div> </li> <li id="comment-110945248" class="comment js-comment " data-comment-id="110945248" data-comment-owner-id="5281051" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110945248_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str I use vuetify. I want my website to support SEO. Each time a page is accessed, the title and description changes according to the page title and description</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment110945248_40388120"><span title="2020-07-05T07:12:20.483 License: CC BY-SA 4.0" class="relativetime-clean">Jul 05 '20 at 07:12</span></a></span> </div> </div> </li> <li id="comment-117161387" class="comment js-comment " data-comment-id="117161387" data-comment-owner-id="1031191" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment117161387_40388120"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@str metaInfo can be also dynamic, declaring `metaInfo()` in child instance.</span> – <a href="../../users/1031191/barney-szabolcs" title="11,846 reputation" class="comment-user ">Barney Szabolcs</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment117161387_40388120"><span title="2021-02-18T23:26:09.443 License: CC BY-SA 4.0" class="relativetime-clean">Feb 18 '21 at 23:26</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="49642890"></a> <div id="answer-49642890" class="answer " data-answerid="49642890" data-ownerid="294702" data-score="68" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="49642890"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="68">68</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>You can do it with 1 line in the App.vue file, like this:</p> <pre><code><script> export default { name: 'app', created () { document.title = "Look Ma!"; } } </script> </code></pre> <p>Or change the <code><title></code> tag content in <code>public/index.html</code></p> <pre><code><!DOCTYPE html> <html> <head> <title>Look Ma!</title> <!- ------ Here -> </head> ... </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2018-04-16T17:35:01.130" class="relativetime">Apr 16 '18 at 17:35</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">MonoThreaded</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 04 '18 at 04:37">answered Apr 04 '18 at 04:37</time> <a href="../../users/294702/monothreaded" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/294702.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="MonoThreaded" /> </a> <div class="s-user-card--info"> <a href="../../users/294702/monothreaded" class="s-user-card--link">MonoThreaded</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">11,429</li> <li class="s-award-bling s-award-bling__gold" title="12 gold badges">12</li> <li class="s-award-bling s-award-bling__silver" title="71 silver badges">71</li> <li class="s-award-bling s-award-bling__bronze" title="102 bronze badges">102</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-49642890" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="49642890" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-104456688" class="comment js-comment " data-comment-id="104456688" data-comment-owner-id="5281051" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment104456688_49642890"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Is this method really useful for SEO? if I right-click on my website and select `View Page Source`, I see the title, description, keywords don't change. I look doubtful this way is correct</span> – <a href="../../users/5281051/moses-toh" title="12,344 reputation" class="comment-user ">moses toh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment104456688_49642890"><span title="2019-11-30T03:11:35.963 License: CC BY-SA 4.0" class="relativetime-clean">Nov 30 '19 at 03:11</span></a></span> </div> </div> </li> <li id="comment-104552697" class="comment js-comment " data-comment-id="104552697" data-comment-owner-id="294702" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment104552697_49642890"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The first one will not do you good. The second is a little better but it will not give you the full benefit of a true in depth optimization. Please note the OP question was NOT about SEO.</span> – <a href="../../users/294702/monothreaded" title="11,429 reputation" class="comment-user ">MonoThreaded</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment104552697_49642890"><span title="2019-12-03T19:48:36.500 License: CC BY-SA 4.0" class="relativetime-clean">Dec 03 '19 at 19:48</span></a></span> </div> </div> </li> <li id="comment-111947544" class="comment js-comment " data-comment-id="111947544" data-comment-owner-id="5078765" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment111947544_49642890"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@positivethinking Modern web crawlers will load JavaScript and wait for the whole webpage to render before examining it. The HTML content is affected when the title property of the document is changed, but typically the source in "view source" never changes after the initial page load. You check to see the HTML changed like this: `document.title = 'whatever'; document.querySelector('title').innerHTML` You can also delete the entire contents of the page using the developer console and see that it's still there in "view source".</span> – <a href="../../users/5078765/adjenks" title="2,973 reputation" class="comment-user ">ADJenks</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment111947544_49642890"><span title="2020-08-07T20:08:35.240 License: CC BY-SA 4.0" class="relativetime-clean">Aug 07 '20 at 20:08</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="36614819"></a> <div id="answer-36614819" class="answer " data-answerid="36614819" data-ownerid="4723237" data-score="6" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="36614819"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="6">6</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>This answer is for vue 1.x</p> <p>using requirejs.</p> <p></p><div class="snippet" data-babel="false" data-console="false" data-hide="false" data-lang="js"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>define([ 'https://cdn.jsdelivr.net/vue/latest/vue.js' ], function(Vue) { var vm = new Vue({ el: 'html', data: { hello: 'Hello world' } }); });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code><!DOCTYPE html> <html id="html"> <head> <title>{{ hello }}</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.js" data-main="app"></script> </head> <body> {{ hello }} <input v-model="hello" title="hello" /> </body> </html></code></pre> </div> </div> <p>you can do it like this using the ready function to set the initial value and watch to update when the data changes.</p> <pre><code><html> <head> <title>Replace Me</title> </head> <body> <script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> <div id="app"> <input v-model="title"> </div> <script> new Vue({ el: '#app', ready: function () { document.title = this.title }, data: { title: 'My Title' }, watch: { title: function (val, old) { document.title = val } } }) </script> </body> </html> </code></pre> <p>also i tried this based on your original code and it works</p> <pre><code><html> <head> <title>{{ title }}</title> </head> <body> <script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> <div id="app"> <input v-model="title"> </div> <script> new Vue({ el: 'html', data: { title: 'My Title' } }) </script> </body> </html> </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2017-12-09T01:54:04.830" class="relativetime">Dec 09 '17 at 01:54</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">vbranden</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 14 '16 at 05:55">answered Apr 14 '16 at 05:55</time> <a href="../../users/4723237/vbranden" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4723237.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="vbranden" /> </a> <div class="s-user-card--info"> <a href="../../users/4723237/vbranden" class="s-user-card--link">vbranden</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">5,814</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="22 silver badges">22</li> <li class="s-award-bling s-award-bling__bronze" title="15 bronze badges">15</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-36614819" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="36614819" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-60826568" class="comment js-comment " data-comment-id="60826568" data-comment-owner-id="2544762" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment60826568_36614819"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Well done, thank you, and I finally found that I was failed because the `el` element is not set correctly(I set it to 'body' in fact). So binding the vm on `html` element can work well!</span> – <a href="../../users/2544762/alfred-huang" title="17,654 reputation" class="comment-user owner">Alfred Huang</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/36612847/how-can-i-bind-the-html-title-content-in-vuejs#comment60826568_36614819"><span title="2016-04-14T06:10:17.700 License: CC BY-SA 3.0" class="relativetime-clean">Apr 14 '16 at 06:10</span></a></span> </div> </div> </li> <li id="comment-64984873" class="comment js-comment " data-comment-id="64984873" data-comment-owner-id="183579" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment64984873_36614819"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You can also scope to just the <title> element, doing el: 'title' – Richard H Aug 06 '16 at 20:10

  • 2
    You get a warning in Vue 2.4.2 : `[Vue warn]: Do not mount Vue to or - mount to normal elements instead.` – user9645 Oct 04 '17 at 12:57
  • @Damian C Is this method really useful for SEO? if I right-click on my website and select `View Page Source`, I see the title, description, keywords don't change. I look doubtful this way is correct – moses toh Nov 30 '19 at 03:11
  • 4

    Just to chime in here. I have read that VueJS wants nothing to do with the meta stuff so I would do such things outside of the "VueJS" realm.

    Basically make a plain vanilla js service like below. Here you could add all the functions to handle the meta data stuff such as the Open Graph data.

    meta.js

    export setTitle(title) {
        document.title = title  
    }
    
    

    Now we can import the service in main and then provide it to any component in the app who wants it. I could even use my meta service in other projects too which use different frameworks like React or Angular. Portability is super cool!

    main.js

    import meta from './meta'
    new Vue({
        router,
        render: h => h(App),
        provide: {
            meta: meta
        }
    }).$mount('#app')
    

    Here the component injects the meta service it wants to use.

    someView.vue

    export default {
        name: 'someView',
        inject: ['meta'],
        data: function() {
            returns {
                title: 'Cool title'
            }
        },
        created: function() {
            this.meta.setTitle(this.title);
        }
    }
    

    This way the meta service is decoupled from the app because different parent components can provide different versions of the meta service. Now you can implement various strategies to see which one is right for you or even different strategies per component.

    Basically the inject walks up the component hierarchy and takes the meta service from the first parent who provides it. As long as the meta service follows a proper interface, you're golden.

    Decoupling with DI is super cool

    mr haven
    • 1,494
    • 1
    • 17
    • 28
    3

    Title and meta tags can be edited and updated asynchronously.

    You can use state management, create a store for SEO using vuex and update each part accordingly.

    Or you can update the element by yourself easily

    created: function() {  
    
      ajax().then(function(data){
         document.title = data.title  
         document.head.querySelector('meta[name=description]').content = data.description
      })
    
    }
    
    serkan
    • 6,885
    • 4
    • 41
    • 49
    2

    If you are using Vuex and want <title> to be part of your application state, then:

    • create a pageTitle state variable in Vuex
    • map the state to the template using mapState()
    • watch it in template, probably add immediate: true to trigger the watcher right away
    • in watcher, document.title = pageTitle

    This will allow you to manage title with Vuex and keep them in sync. I found it useful for SPAs.

    By doing this you don't have to mess with your original HTML template, as most of the time Vue root template resides inside <body>.

    This is for Vue 2.x.

    dz902
    • 4,782
    • 38
    • 41
    0
    router.beforeEach((to, from, next) => {
      let mohican = to.path; if (mohican == '/') mohican = 'Home'
      document.title =  mohican.replace('/','');
      next();
     return;
    });
    
    Dr.Sai
    • 303
    • 4
    • 5
    0

    I have an application toolbar component which is common for all pages of my SPA website and is nested in App.vue. In every page I update my common toolbar title in the created hook of the page using Vuex store:

    //in every page.vue
    created() {
      this.$store.commit('toolBar', { pageTitle: this.pageTitle, ...  })
    },
    

    To automatically update the website title (along with the toolbar title) I use this mutation in the store:

    //store.js
    toolBar(state,val){
      document.title = val.pageTitle
      state.toolBar = val
    },
    

    Similarly, I use the same mechanism to update e.g. SEO metadata

    vess
    • 502
    • 7
    • 11
    0

    just pass

    :title="data.name"

    Dinesh Vishwakarma
    • 656
    • 3
    • 13
    • 34