Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind the html <title> content in vuejs?

I'm trying a demo on vuejs. Now I want the html title to bind a vm field.

The below is what I tried:

index.html

<!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> 

app.js

define([     'jquery', 'vue' ], function ($, Vue) {     var vm = new Vue({         el: 'html',         data: {             hello: 'Hello world'         }     }); }); 

But the title seemed not bounded, how to make it work?

like image 705
Alfred Huang Avatar asked Apr 14 '16 02:04

Alfred Huang


People also ask

How do you link Vue and HTML?

The simplest way to get started with Vue is to grab the development version script for it and add it to the head tag of your HTML file. Then you can start Vue code inside the HTML file inside of script tags. And have the Vue code connect up with an existing element on your HTML page.

How do you bind variables in Vue?

If we want to bind any variable, we can simply use Vue. js's double curly braces syntax or “Mustache” syntax to bind any variable from the relative component instance. Or, if we want to bind any variable inside an HTML attribute, we can use the v-bind directive.

What is V-bind title?

“v-bind title” Code Answer's The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive.


2 Answers

There are essentially two ways to solve it.

Use an existing Package

For example, vue-meta:

<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> 

Create your own Component

Create a vue file containing:

<script>     export default {         name: 'vue-title',         props: ['title'],         watch: {             title: {                 immediate: true,                 handler() {                     document.title = this.title;                 }             }         },         render () {         },     } </script> 

Register the component using

import titleComponent from './title.component.vue'; Vue.component('vue-title', titleComponent); 

Then you can use it in your templates, e.g.

<vue-title title="Static Title"></vue-title> <vue-title :title="dynamic.something + ' - Static'"></vue-title> 
like image 114
str Avatar answered Sep 20 '22 22:09

str


You can do it with 1 line in the App.vue file, like this:

<script>     export default {         name: 'app',         created () {             document.title = "Look Ma!";         }     } </script> 

Or change the <title> tag content in public/index.html

<!DOCTYPE html> <html>   <head>     <title>Look Ma!</title> <!- ------ Here ->   </head> ... 
like image 29
MonoThreaded Avatar answered Sep 22 '22 22:09

MonoThreaded