Man*_*piK 6 vue.js nuxt.js vue-meta
在页面中,我设置标题如下:
...
<script>
export default {
head() {
return {
title: this.post.name
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如何在另一个组件中获取这些数据?
我尝试过,this.$metaInfo但我需要获取数据的组件位于外部布局中<nuxt />...
另外,如果当前路由位于填充了头部的子页面中,则它将覆盖父标题。那么,我该怎么办呢?
this.$metaInfo只能在页面组件中访问。如果您想在任何地方拥有当前页面的标题,我认为最好的方法是使用store保存当前标题,然后在任何组件中轻松检索此信息。
在商店/index.js
export const state = {
title: 'Default Title'
}
export const mutations = {
SET_TITLE (state, title) {
state.title= title
}
}
Run Code Online (Sandbox Code Playgroud)
然后在页面组件上使用它
<template>
<div></div>
</template>
<script>
export default {
head () {
return {
title: this.title
}
},
mounted () {
this.$store.commit('SET_TITLE', this.$metaInfo.title)
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
现在,您可以在从商店状态检索当前标题的任何组件中访问当前标题。
<template>
<div></div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState({
title: state => state.title
})
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6250 次 |
| 最近记录: |