-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
在 VitePress 中,`public` 目录中的文件会被直接复制到构建输出的根目录。因此,在引用 `public` 目录中的文件时,不需要在路径中包含 `public` 这一部分。 | ||
|
||
### 详细解释 | ||
|
||
1. **`public` 目录的作用**: | ||
- `public` 目录中的文件会在构建时被直接复制到输出目录的根目录。 | ||
- 这些文件可以通过根路径直接访问。 | ||
|
||
2. **路径引用**: | ||
- 由于 `public` 目录中的文件在构建后会位于根目录,因此在引用这些文件时,只需要使用相对于根目录的路径。 | ||
- 例如,`public/img/focus.png` 在构建后会变成 `/img/focus.png`。 | ||
|
||
### 示例 | ||
|
||
假设你的项目结构如下: | ||
``` | ||
D:\ | ||
└─ A_Vue\ | ||
└─ githubvitepress\ | ||
├─ doc\ | ||
│ ├─ .vitepress\ | ||
│ │ ├─ config.mts | ||
│ ├─ public\ | ||
│ │ └─ img\ | ||
│ │ └─ focus.png | ||
│ ├─ index.md | ||
├─ package.json | ||
└─ ... | ||
``` | ||
|
||
在 `index.md` 文件中引用 `public/img` 目录中的图片时,只需要使用绝对路径 `/img/focus.png`: | ||
|
||
```markdown | ||
![focus](/img/focus.png) | ||
``` | ||
|
||
### 配置 `base` 选项 | ||
|
||
确保在 `.vitepress/config.mts` 文件中正确配置 `base` 选项。如果你的站点部署在根路径下,`base` 应该设置为 `'/'`: | ||
|
||
```typescript | ||
import { defineConfig } from 'vitepress'; | ||
|
||
export default defineConfig({ | ||
base: '/', // 如果部署在根路径下 | ||
}); | ||
``` | ||
|
||
## 总结: | ||
**引入public中的内容,只需要在路径前加上`/`即可,不需要加上`public`这一部分。但是需要加上`public`中的文件夹名字,比如`/img`。** | ||
|