【发布时间】:2018-01-06 19:24:57
【问题描述】:
我正在使用基于 vue.js 的 nuxt.js 开发一个网站。该站点是一个简单的站点,其中包含许多有关内容的页面。我想创建一个组件,它知道它在内容中的位置,因此需要访问路由器对象。我试过$nuxt.$route.path,但出现错误。如何导入 $nuxt 或 $router 对象以在我的组件中访问它们?
我的模板组件
<template>
<div>
<hr>
<p>{{ $store.state.menuitems[0] }}</p>
<hr>
<div class="artfooter">
<span>i want name of current route here</span>
</div>
<br>
</div>
</template>
<style scoped>
.artfooter{
font-size: 0.8em;
display: flex;
justify-content: space-between;
}
</style>
<script>
import store from '~/store/index'
//how to import $nuxt object to access router??
console.log(store)
console.log(this.$router.path)
// the above console.log reports error
// Cannot read property 'middleware' of undefined
export default {
}
</script>
【问题讨论】:
-
您的错误是什么?您能否包含一些代码示例,您是如何尝试从您的组件中访问它的?
标签: javascript vuejs2 frontend nuxt.js