Skip to content

Commit ca7beeb

Browse files
committed
docs: add making-your-theme-extendable section
1 parent 3ce4d84 commit ca7beeb

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

docs/advanced/cookbook/extending-a-theme.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,71 @@ Here are all the slots that provided by the `Layout` of default theme:
7777
- `sidebar-bottom`
7878
- `page-top`
7979
- `page-bottom`
80+
81+
## Making Your Theme Extendable
82+
83+
As a theme author, you might want to make your theme extendable, allowing users to use your theme with their own customization.
84+
85+
You can provide slots in your layouts, just like how default theme does. This approach requires you to determine which parts of your theme could be extended. It is more suitable for those common customizations like page footer or header:
86+
87+
```vue
88+
<template>
89+
<div class="my-theme-layout">
90+
<slot name="page-header" />
91+
<Content />
92+
<slot name="page-footer" />
93+
</div>
94+
</template>
95+
```
96+
97+
If you think it is not flexible enough, you can try some more aggressive approaches to make each components of you theme replaceable.
98+
99+
For example, set `alias` for each components of you theme:
100+
101+
```js
102+
module.exports = {
103+
name: 'vuepress-theme-foo',
104+
alias: {
105+
// set alias for replaceable components
106+
'@theme/Navbar.vue': path.resolve(__dirname, 'components/Navbar.vue'),
107+
'@theme/Sidebar.vue': path.resolve(__dirname, 'components/Sidebar.vue'),
108+
},
109+
}
110+
```
111+
112+
Next, use those components via aliases in your theme:
113+
114+
```vue
115+
<template>
116+
<div class="my-theme-layout">
117+
<Navbar />
118+
<Sidebar />
119+
<Content />
120+
</div>
121+
</template>
122+
123+
<script>
124+
import Navbar from '@theme/Navbar.vue'
125+
import Sidebar from '@theme/Sidebar.vue'
126+
127+
export default {
128+
components: {
129+
Navbar,
130+
Sidebar,
131+
},
132+
}
133+
</script>
134+
```
135+
136+
Then, users can replace specific components by overriding the `alias` when extending your theme:
137+
138+
```js
139+
module.exports = {
140+
name: 'vuepress-theme-foobar',
141+
extends: 'vuepress-theme-foo'
142+
alias: {
143+
// replace the Navbar component
144+
'@theme/Navbar.vue': path.resolve(__dirname, 'components/CustomNavbar.vue'),
145+
},
146+
}
147+
```

docs/zh/advanced/cookbook/extending-a-theme.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,71 @@ export default {
7777
- `sidebar-bottom`
7878
- `page-top`
7979
- `page-bottom`
80+
81+
## 使你的主题可以被继承
82+
83+
作为一个主题作者,为了允许用户在使用你的主题时进行更多的自定义,你可能希望你的主题可以被用户继承。
84+
85+
你可以像默认主题的做法一样,在你的布局中添加插槽。这种方式需要你来决定主题的哪些部分是可以被扩展的,它更适合用于一些常见的自定义需求,比如页眉或页脚:
86+
87+
```vue
88+
<template>
89+
<div class="my-theme-layout">
90+
<slot name="page-header" />
91+
<Content />
92+
<slot name="page-footer" />
93+
</div>
94+
</template>
95+
```
96+
97+
如果你觉得这种方式还不够灵活,你可以尝试一些更激进的做法,使你主题的每个组件都可以被替换。
98+
99+
比如,为你主题的每个组件都设置 `alias` 别名:
100+
101+
```js
102+
module.exports = {
103+
name: 'vuepress-theme-foo',
104+
alias: {
105+
// 为可替换的组件设置别名
106+
'@theme/Navbar.vue': path.resolve(__dirname, 'components/Navbar.vue'),
107+
'@theme/Sidebar.vue': path.resolve(__dirname, 'components/Sidebar.vue'),
108+
},
109+
}
110+
```
111+
112+
然后,在你的主题中通过别名来使用这些组件:
113+
114+
```vue
115+
<template>
116+
<div class="my-theme-layout">
117+
<Navbar />
118+
<Sidebar />
119+
<Content />
120+
</div>
121+
</template>
122+
123+
<script>
124+
import Navbar from '@theme/Navbar.vue'
125+
import Sidebar from '@theme/Sidebar.vue'
126+
127+
export default {
128+
components: {
129+
Navbar,
130+
Sidebar,
131+
},
132+
}
133+
</script>
134+
```
135+
136+
这样,用户在继承你的主题时,就可以通过覆盖 `alias` 来替换特定的组件了:
137+
138+
```js
139+
module.exports = {
140+
name: 'vuepress-theme-foobar',
141+
extends: 'vuepress-theme-foo'
142+
alias: {
143+
// 替换 Navbar 组件
144+
'@theme/Navbar.vue': path.resolve(__dirname, 'components/CustomNavbar.vue'),
145+
},
146+
}
147+
```

0 commit comments

Comments
 (0)