非常感谢@leas。由于我设法在他们的帮助下让它工作,我将分享一个覆盖组件的具体案例,而不是默认主题。
在这种情况下,我想在主题 @vuepress/theme-blog 上覆盖 Header.vue,并且我还需要增强基本主题配置。
我想要做的就是在我的导航栏上添加一个徽标,核心 vuepress 支持但 @vuepress/theme-blog 不支持。
我事先对 components/Header.vue 所做的事情
我通过在 node_modules 下直接修改它添加了一个<img> 标签并让它成功显示我的徽标,但后来我正在寻找一种不必在每次 npm 后覆盖它的方法安装。
<div class="header-wrapper">
<div class="title">
+ ? <img v-if="$themeConfig.logo" :src="$themeConfig.logo" alt="logo">
<NavLink link="/" class="home-link">{{ $site.title }} </NavLink>
</div>
<div class="header-right-wrap">
开始 .vuepress 下的目录层次结构
(减去 public 和其他来自 tree -a -I 'public|about|_issues|_posts' 的东西)
.
└── .vuepress
└── config.js
final 目录层次结构,遵循 leas 的回答:
.
└── .vuepress
├── config.js
└── theme
├── components
│ └── Header.vue
└── index.js
文件内容及调整:
index.js:
module.exports = {
extend: '@vuepress/theme-blog'
}
Header.vue,调整继承后:
img 保持不变,但请注意 @parent-theme 并且我不能将 ./Feed 用作兄弟,但必须引用 components 目录。
+ <img v-if="$themeConfig.logo" :src="$themeConfig.logo" alt="logo">
....
< import Feed from './Feed'
---
> import Feed from '@parent-theme/components/Feed' ?
我怎么知道我必须这样做?好吧,该页面无法正常工作,并且 console.log 显示了 ./Feed 周围的编译错误。按照建议查看https://v1.vuepress.vuejs.org/theme/inheritance.html#inheritance-strategy。
config.js
这包括两件事:
module.exports = {
...
! // theme: '@vuepress/theme-blog', ? theme/index.js handles that.
...
themeConfig: {
+ logo: "/selectobjects.png", ? and the modified Header.vue uses this.
注意:我将复制一份核心 Header.vue 并保存。将来,如果 @vuepress/theme-blog 修改了他们的,我希望能够看到我在这里的原始定制与他们当时的核心组件相比如何。
使用的仅供参考的版本:
vuepress@1.5.2
@vuepress/theme-blog@2.2.0
npm --version
6.14.5
node --version
v10.15.0