it-swarm.com.de

vue.js ruft den Routennamen in App.vue ab

Ich habe vue-cli mit webpack verwendet, um das vue-Projekt aufzubauen. Dann installierte ich vue-meta-info, um das seo einzurichten.

Ich möchte den Seitentitel mit den Vorlagen und dem Routennamen einrichten. Die Variable kann jedoch nicht im Router abgerufen werden.

rotuer/index.js

import Vue from 'vue';
import Router from 'vue-router';
import HelloWorld from '@/components/HelloWorld';

Vue.use(Router);

export default new Router({
      mode: 'history',
      routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld,
    },
  ],
});

App.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
 name: 'App',
 metaInfo: {
    title: "My Website - "+ route.name,
},
};

</script>
5
Murasaki Aikon

Sie können den beforeEach()-Helfer des Routers verwenden.

Beispiel:

routen.js

import Foo from '@/components/Foo'

export default new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Foo',
      component: Foo,
      meta: {
        title: 'Foo'
      }
    }
  ]
})

In app.js oder main.js oder was auch immer Ihre Haupt-Javascript-Datei ist. Wenn Sie den Code in eine der oben genannten JS-Dateien einfügen, können alle Seiten den Titel entsprechend aktualisieren. Dies ist eine viel sauberere Art, die Titel zu bearbeiten.

import router from '@/routes'

// your other codes here

router.beforeEach((to, from, next) => {
  document.title = `My website - ${to.meta.title}`
  next()
})
5
Ru Chern Chong

Sie müssen den Titel der Änderungen von jeder Komponente an die Stammkomponente vornehmen. Eine Möglichkeit ist $on .... $emit.

const Door = { template: '<div>Door</div>',
		mounted: function () {
    	vm.$emit('title', this.$route.name);
    } 
  }
const Bedroom = { template: '<div>Bedroom</div>',
		mounted: function () {
      vm.$emit('title', this.$route.name);
    } 
  }
const Kitchen = { template: '<div>Kitchen</div>',
		mounted: function () {
      vm.$emit('title', this.$route.name);
    } 
  }
const Hall = { template: '<div>Hall</div>',
		mounted: function () {
      vm.$emit('title', this.$route.name);
    } 
  }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: Door, name:"You are in door"},
    { path: '/bedroom', component: Bedroom, name:"You are in Bedroom"},
    { path: '/kitchen', component: Kitchen, name:"You are in kitchen"},
    { path: '/hall', component: Hall, name:"You are in hall"},
  ]
})

var vm = new Vue({
	router,
  el: '#app',
  data: {
    msg: 'Hello World',
    title:'no title set'
  },
  mounted: function(){
  this.$on('title', function (title) {
    this.title = title;
  })
  }
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <div>Title : {{title}}</div>
  <router-link to="/">Door</router-link>
  <router-link to="/bedroom"> | Bedroom</router-link>
  <router-link to="/kitchen"> | Kitchen</router-link>
  <router-link to="/hall"> | Hall</router-link>
  <router-view></router-view>
</div>

Geige arbeiten ist

Eine andere Möglichkeit ist $parent,

const Door = { template: '<div>Door</div>',
		mounted: function () {
      this.$parent.title = this.$route.name;
    } 
  }
const Bedroom = { template: '<div>Bedroom</div>',
		mounted: function () {
      this.$parent.title = this.$route.name;
    } 
  }
const Kitchen = { template: '<div>Kitchen</div>',
		mounted: function () {
      this.$parent.title = this.$route.name;
    } 
  }
const Hall = { template: '<div>Hall</div>',
		mounted: function () {
      this.$parent.title = this.$route.name;
    } 
  }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: Door, name:"You are in door"},
    { path: '/bedroom', component: Bedroom, name:"You are in Bedroom"},
    { path: '/kitchen', component: Kitchen, name:"You are in kitchen"},
    { path: '/hall', component: Hall, name:"You are in hall"},
  ]
})

new Vue({
	router,
  el: '#app',
  data: {
    msg: 'Hello World',
    title:'no title set'
  }
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <div>Title : {{title}}</div>
  <router-link to="/">Door</router-link>
  <router-link to="/bedroom"> | Bedroom</router-link>
  <router-link to="/kitchen"> | Kitchen</router-link>
  <router-link to="/hall"> | Hall</router-link>
  <router-view></router-view>
</div>

Working JSFiddle

0
Niklesh Raut