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

后端 未结 5 472
后悔当初
后悔当初 2020-11-29 00:19

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



        
相关标签:
5条回答
  • 2020-11-29 00:40

    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>
    ...
    
    0 讨论(0)
  • 2020-11-29 00:41

    This answer is for vue 1.x

    using requirejs.

    define([
      'https://cdn.jsdelivr.net/vue/latest/vue.js'
    ], function(Vue) {
      var vm = new Vue({
        el: 'html',
        data: {
          hello: 'Hello world'
        }
      });
    });
    <!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>

    you can do it like this using the ready function to set the initial value and watch to update when the data changes.

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

    also i tried this based on your original code and it works

    <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>
    
    0 讨论(0)
  • 2020-11-29 00:48

    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
      })
    
    }
    
    0 讨论(0)
  • 2020-11-29 00:49

    There are essentially two ways to solve it.

    Use an existing Package

    For example, vue-meta:

    <template>
        <div>
            <vue-headful
                title="Title from vue-headful"
                description="Description from vue-headful"
            />
        </div>
    </template>
    
    <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 views, e.g.

    <vue-title title="Static Title"></vue-title>
    <vue-title :title="dynamic.something + ' - Static'"></vue-title>
    
    0 讨论(0)
  • 2020-11-29 00:57

    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

    0 讨论(0)
提交回复
热议问题