javascript vue.js 在 App.vue 中获取路由名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49685780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:43:01  来源:igfitidea点击:

vue.js get route name in App.vue

javascriptvue.jsvuejs2vue-router

提问by Murasaki Aikon

I used vue-cli with webpack to build up the vue project. Then I installed vue-meta-info to set up the seo.

我使用 vue-cli 和 webpack 来构建 vue 项目。然后我安装了 vue-meta-info 来设置 seo。

I want to set up the Page title with the templates and the route name. However , I cannot get the variable in router.

我想用模板和路线名称设置页面标题。但是,我无法在路由器中获取变量。

rotuer/index.js

路由器/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>

回答by Ru Chern Chong

You can use the router's beforeEach()helper.

您可以使用路由器的beforeEach()助手。

Example:

例子:

routes.js

路由.js

import Foo from '@/components/Foo'

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

In app.jsor main.jsor whatever your main Javascript file is. Placing the code in one of the aforementioned JS file will allow all pages to update the title accordingly and is a much cleaner way to handle the titles.

app.jsmain.js任何您的主要 Javascript 文件中。将代码放入上述 JS 文件之一将允许所有页面相应地更新标题,并且是处理标题的更简洁的方式。

import router from '@/routes'

// your other codes here

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

回答by Niklesh Raut

You need to make changes title from every component to root component, One way is by $on .... $emit,

您需要将标题从每个组件更改为根组件,一种方法是通过$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>

Working fiddle is

工作小提琴是

Another way is using $parent,

另一种方法是使用$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

工作 JSFiddle