W
W
wickinats2020-05-10 10:38:36
Frontend
wickinats, 2020-05-10 10:38:36

How to properly designate Title for dynamic pages in VueJS?

// Router
{
    path: '/user/:id',
    name: 'User',
    component: User,

    meta: { title: ЗДЕСЬ НУЖНО ОБОЗНАЧИТЬ ID пользователя }
 }


I would also like to know how to correctly specify the title of the page in Vue? Now I am using the following design.
// App.vue
  import Menu from './components/Menu'

  export default {
    name: 'Artefact',
    components: {
      Menu
    },

    computed: {
      pageTitle() { return this.$route.meta.title; }
    },

    watch: {
      pageTitle: (val) => {
        document.title = val;
      }
    }, created() {
      document.title = this.$route.meta.title;
    }
  }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimasik30, 2020-05-11
@wickinats

Use vue-meta

S
Stas Sych, 2020-05-11
@StasSych

Why not use router.beforeEach
This will save you the competed i from watch
https://router.vuejs.org/ru/guide/advanced/navigat...

I
Ibishka, 2020-05-10
@Ibishka

// App.vue
  import Menu from './components/Menu'

  export default {
    name: 'Artefact',
    components: {
      Menu
    },
    methods: {
      getPost() {
        fetch('db.json')
          .then(res => res.json())
          .then(users=> {
document.title = `user ${users.id}`;
.......
        })
      }
    },
    created() {
      this.getPost()
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question