您正在阅读 Nuxt 内容 V1 文档。阅读最新版本
获取内容
此模块全局注入 $content 实例,这意味着您可以使用 this.$content 在任何地方访问它。对于插件、asyncData、nuxtServerInit 和 Middleware,您可以从 context.$content 访问它。
方法
$content(path, options?)
- 路径- 类型: string
- 默认值: /
 
- 类型: 
- 选项- 类型: object
- 默认值: {}
- 版本: >= v1.3.0
 
- 类型: 
- options.deep- 类型: boolean
- 默认值: false
- 版本: >= v1.3.0
- 从子目录获取文件
 
- 类型: 
- options.text- 类型: boolean
- 默认值: false
- 版本: >= v1.4.0
- 在 text变量中返回原始的 Markdown 内容
 
- 类型: 
- 返回一个链式序列
您也可以给出多个参数:
$content('articles', params.slug)将被转换为/articles/${params.slug}
path 可以是文件或目录。如果 path 是文件,fetch() 将返回一个 object,如果它是目录,它将返回一个 Array。
以下所有方法都可以链接并返回一个链式序列,除了 fetch 返回一个 Promise。
only(keys)
- 密钥- 类型: Array|string
- 必需
 
- 类型: 
选择一个字段子集。
const { title } = await this.$content('article-1').only(['title']).fetch()
without(keys)
- 密钥- 类型: Array|string
- 必需
 
- 类型: 
删除一个字段子集。
const { title, ...propsWithoutBody } = await this.$content('article-1').without(['body']).fetch()
where(query)
- 查询- 类型: object
- 必需
 
- 类型: 
通过查询过滤结果。
Where 查询基于 mongo 查询语法的子集,它处理例如: $eq, $ne, $gt, $gte, $lt, $lte, $in, ...
// implicit (assumes $eq operator)
const articles = await this.$content('articles').where({ title: 'Home' }).fetch()
// explicit $eq
const articles = await this.$content('articles').where({ title: { $eq: 'Home' } }).fetch()
// $gt
const articles = await this.$content('articles').where({ age: { $gt: 18 } }).fetch()
// $lte
const articles = await this.$content('articles').where({ createdDateTime: { $lte: new Date().toISOString() } }).fetch()
// $in
const articles = await this.$content('articles').where({ name: { $in: ['odin', 'thor'] } }).fetch()
为了在对象和数组中进行过滤,您需要启用 nestedProperties,请参见 配置。
const products = await this.$content('products').where({ 'categories.slug': { $contains: 'top' } }).fetch()
const products = await this.$content('products').where({ 'categories.slug': { $contains: ['top', 'woman'] } }).fetch()
此模块在幕后使用 LokiJS,您可以查看 查询示例。
sortBy(key, direction)
- 密钥- 类型: string
- 必需
 
- 类型: 
- 方向- 类型: string
- 值: 'asc'或'desc'
- 默认值: 'asc'
 
- 类型: 
按密钥排序结果。
const articles = await this.$content('articles').sortBy('title').fetch()
可以多次链接以按多个字段排序。
sortBy 方法执行 **区分大小写** 的排序,目前不可配置。
如果您需要不区分大小写的排序,请查看 此代码片段,了解如何解决此问题。
limit(n)
- n- 类型: string|number
- 必需
 
- 类型: 
限制结果数量。
// fetch only 5 articles
const articles = await this.$content('articles').limit(5).fetch()
skip(n)
- n- 类型: string|number
- 必需
 
- 类型: 
跳过结果。
// fetch the next 5 articles
const articles = await this.$content('articles').skip(5).limit(5).fetch()
search(field, value)
- 字段- 类型: string
- 必需
 
- 类型: 
- 值- 类型: string
 
- 类型: 
对字段执行全文搜索。 value 是可选的,在这种情况下,field 是 value,并且将在所有定义的全文搜索字段上执行搜索。
您要搜索的字段必须在选项中定义才能被索引,请参见 配置。
使用空字符串作为参数将跳过搜索。
// Search on field title
const articles = await this.$content('articles').search('title', 'welcome').fetch()
// Search on all pre-defined fields
const articles = await this.$content('articles').search('welcome').fetch()
// Search will be skipped if the search string is empty
const articles = await this.$content('articles').search('').fetch()
查看 此代码片段,了解如何在您的应用程序中实现搜索
surround(slugOrPath, options)
- slugOrPath- 类型: string
- 必需
 
- 类型: 
- 选项- 类型: object
- 默认值: { before: 1, after: 1}
 
- 类型: 
获取围绕特定 slug 或路径的之前和之后的结果。
您将始终获得一个固定长度的数组,其中包含匹配的文档或 null。
const [prev, next] = await this.$content('articles')
  .only(['title', 'path'])
  .sortBy('date')
  .where({ isArchived: false })
  .surround('article-2')
  .fetch()
// Returns
[
  {
    title: 'Article 1',
    path: 'article-1'
  },
  null // no article-3 here
]
search,limit和skip在使用此方法时无效。
基于 path 获取结果仅在 v1.12.0 及更高版本中支持
查看 此代码片段,了解如何在您的应用程序中实现之前和之后的链接
fetch()
- 返回: Promise<object>|Promise<Array>
结束链式序列并收集数据。
catch()
检查内容目录中是否存在 .md 文件。
它应该插入在 fetch() 之后。
示例
const articles = await this.$content('articles')
  .only(['title', 'date', 'authors'])
  .sortBy('date', 'asc')
  .limit(5)
  .skip(10)
  .where({
    tags: 'testing',
    isArchived: false,
    date: { $gt: new Date('2020-03-31') },
    rating: { $gte: 3 }
  })
  .search('welcome')
  .fetch()
  .catch((err) => {
     error({ statusCode: 404, message: 'Page not found' })
  })
您可以查看如何在开发中使用 内容 API。