要定义可以在站点中的任何位置访问的某些变量,可以将它们添加到主题配置中。
如果您还没有,请创建一个 config.js 档案在 .vuepress/config.js 。
config.js
.vuepress/config.js
此文件应导出对象。
你想添加一个 themeConfig: {} 对此。
themeConfig: {}
您在上面设置的属性 themeConfig 对象将在您的整个网站上可用 $themeConfig 。
themeConfig
$themeConfig
//- .vuepress/config.js module.exports = { themeConfig: { //- Define your variables here author: 'Name', foo: 'bar' } }
{{ $themeConfig.author }} //- 'Name' {{ $themeConfig.foo }} //- 'bar
您还可以通过使用全局计算函数轻松地在本地/每页覆盖。 (这也可以提供更简洁的方式来访问变量)
添加一个 enhanceApp.js 文件在同一个地方 config.js ,将允许您访问Vue实例 - 您可以在其中为所有组件定义mixin。
enhanceApp.js
您可以在此mixin中定义一些计算属性,首先检查页面前端数据中的值,然后回退到themeConfig中设置的值。允许您设置一些可以在每页本地覆盖的默认值。
//- .vuepress/enhanceApp.js export default ({ Vue }) => { Vue.mixin({ computed: { author() { const { $themeConfig, $frontmatter } = this return $frontmatter.author || $themeConfig.author }, foo() { const { $themeConfig, $frontmatter } = this return $frontmatter.foo || $themeConfig.foo } } }) }
{{ author }} //- 'Name' {{ foo }} //- 'bar
Vuepress配置文档 Vuepress应用程序级别增强