このモジュールを導入することで、簡単にNuxt.jsプロジェクトにSpearly CMSのコンテンツを表示したりすることができるようになります。
@spearly/nuxt-module
インストール方法
以下のいずれかを使用して、Nuxt.jsのプロジェクトにインストールします。$ npm i -S @spearly/nuxt-module
$ yarn add @spearly/nuxt-moduleNuxt3を使用している場合
Nuxt3版は現在next版として提供しているため、以下でインストールしてください。$ npm i -S @spearly/nuxt-module@^1.0.0-next.0
$ yarn add @spearly/nuxt-module@^1.0.0-next.0なお、Nuxt3は現在RC版のため、Nuxt3の開発次第ではモジュールが動かなくなることがあります。あらかじめご了承ください。セットアップ
nuxt.config.js(TypeScriptを使用している場合はnuxt.config.ts)に以下の記述を追加します。export default {
...,
buildModules: ['@spearly/nuxt-module'],
spearly: {
options: {
apiKey: 'YOUR_API_KEY',
},
mode: 'all',
},
}また、TypeScriptを使用している場合はtsconfig.jsonに以下を追記してください。{
"compilerOptions": {
"types": ["@spearly/nuxt-module"]
}
}Nuxt3を使用している場合
Nuxt3の場合は、nuxt.config.tsの書き方が以下になります。export default defineNuxtConfig({
...,
modules: ['@spearly/nuxt-module', { apiKey: 'YOUR_API_KEY' }],
})※ YOUR_API_KEYはSpearly CMSのAPIキーを設定してください。spearly-content-listコンポーネント
spearly-content-listコンポーネントは、コンテンツ一覧を描画するコンポーネントです。props一覧
| name | type ()はデフォルト値 | 説明 |
|---|---|---|
| id *必須 | string | 取得するコンテンツタイプのIDを指定します。public_uidもしくはidentifierを指定してください。 |
| limit | number(10) | 取得件数を制限します。 |
| offset | number(0) | 取得開始位置を指定します。 |
| order | 'desc', 'asc'('desc') | ソートの指定です。published_atに対して行われます。order-byとの併用はできません。 |
| order-by | 'latest', 'popular', string | latestは公開日時、popularは閲覧数の降順でソートします。 |
| filter-by | string | 特定のフィールドと指定する値でフィルタリングします。filter-byはフィルタリングしたいフィールドのIDを指定します。 filter-valueとセットで使用します。 |
| filter-value | string | フィルタリングしたいフィールドの値を指定します。部分一致で検索されます。 filter-byとセットで使用します。 |
| filter-ref | string | 参照コンテンツに対するフィルタリングです。filter-refには参照コンテンツのフィールドIDを指定し、filter-by、filter-valueで参照先のコンテンツのフィールドID、値を指定します。 |
| range-to | string | 指定した日付より以前に公開されたコンテンツを取得します。 |
| range-from | string | 指定した日付より後に公開されたコンテンツを取得します。 |
| wrapper | string | 一覧のラッパーコンポーネントを指定できます。詳しくは「ラッパーコンポーネントの置き換え」をご覧ください。 |
| item | string | コンテンツのラッパーコンポーネントを指定できます。詳しくは「ラッパーコンポーネントの置き換え」をご覧ください。 |
| loading | string | ローディング時に表示するコンポーネントを指定できます。詳しくは「ローディングの表示」をご覧ください。 |
基本的なコード例
<template>
<spearly-content-list id="CONTENT_TYPE_IDENTIFIER">
<template v-slot="item"> // コンテンツのデータを受け取ることができます。
<nuxt-link :to="`/${item.content.attributes.publicUid}`">
{{ item.content.values.title }}
</nuxt-link>
</template>
</spearly-content-list>
</template>ラッパーコンポーネントの置き換え
一覧のラッパーのコンポーネントと各コンテンツのラッパーのコンポーネントは、以下のpropを使用することで独自のコンポーネントに変更できます。- wrapper prop:一覧全体のラッパーのコンポーネント名もしくはHTMLを指定できます。
- item prop:コンテンツのコンポーネント名もしくはHTMLを指定できます。
<template>
<spearly-content-list id="CONTENT_TYPE_IDENTIFIER" wrapper="ContentList" item="ContentItem">
<template v-slot="item">
<nuxt-link :to="`/${item.content.attributes.publicUid}`">
{{ item.content.values.title }}
</nuxt-link>
</template>
</spearly-content-list>
</template>ページネーションの作成
ページネーション作成用に、pager slotが用意されています。pager slotでは以下のページングに必要なスロットプロパティを受け取ることができるため、これを使ってページネーションを作成することができます。
- limit(number):取得件数
- offset(number):取得時に使用したオフセット
- next(number):次のページのオフセット
- matchingContentsCount(number):取得条件にマッチしたコンテンツの件数
- totalCountentsCount(number):コンテンツタイプに登録されたコンテンツの件数
<template>
<spearly-content-list id="CONTENT_TYPE_IDENTIFIER" :limit="10" :offset="offset">
<template v-slot="item">
<nuxt-link :to="`/${item.content.attributes.publicUid}`">
{{ item.content.values.title }}
</nuxt-link>
</template>
<template #pager="data">
<button v-if="data.paging.offset > 0" @click="offset -= 10">前へ</button>
<button v-if="data.paging.next > 0" @click="offset += 10">次へ</button>
</template>
</spearly-content-list>
</template>ローディングの表示
loading propを使用することで、記事の読み込み中にローディングを表示させることができます。<template>
<spearly-content-list id="CONTENT_TYPE_IDENTIFIER" loading="Loading">
<template v-slot="item">
<nuxt-link :to="`/${item.content.attributes.publicUid}`">
{{ item.content.values.title }}
</nuxt-link>
</template>
</spearly-content-list>
</template>spearly-contentコンポーネント
spearly-contentはコンテンツを描画するためのコンポーネントです。コンテンツ詳細ページなどでの使用を想定しています。
props一覧
| name | type | 説明 |
|---|---|---|
| id *必須 | string | Spearly CMSのエイリアスなどに表示されているコンテンツのIDを指定してください。 |
| loading | string | ローディング時に表示するコンポーネントを指定できます。詳しくは「ローディングの表示」をご覧ください。 |
| preview-token | string | コンテンツのプレビューを表示したい場合に指定します。Spearly CMSで発行されるプレビュートークンを指定してください。 |
基本的なコード例
<template>
<spearly-content id="CONTENT_PUBLIC_UID">
<template v-slot="content"> // コンテンツのデータを受け取ることができます。
<header>
<h1>{{ content.values.title }}</h1>
</header>
<div v-html="content.values.body" />
</template>
</spearly-content>
</template>ローディングの表示
loading propを使用することで、記事の読み込み中にローディングを表示させることができます。<template>
<spearly-content id="CONTENT_PUBLIC_UID" loading="Loading">
<template v-slot="content">
<header>
<h1>{{ content.values.title }}</h1>
</header>
<div v-html="content.values.body" />
</template>
</spearly-content>
</template>プレビューの表示
preview-token propを指定することで、下書きのコンテンツをプレビューすることができます。<template>
<spearly-content :id="$route.query.content_id" :preview-token="$route.query.preview_token">
<template v-slot="content">
<header>
<h1>{{ content.values.title }}</h1>
</header>
<div v-html="content.values.body" />
</template>
</spearly-content>
</template>上記の例はURL QueriesでコンテンツIDとプレビュートークンを受け取って表示することを想定しています。spearly-formコンポーネント
spearly-formは、Spearly CMSで作成したフォームの表示、入力内容の確認、送信までを行うことのできるコンポーネントです。props一覧
| name | type | 説明 |
|---|---|---|
| id *必須 | string | Spearly CMSの埋め込み用フォームIDを指定してください。 |
| loading | string | コンテンツのプレビューを表示したい場合に指定します。Spearly CMSで発行されるプレビュートークンを指定してください。 |
基本的なコード例
<template>
<spearly-form id="FORM_ID" />
</template>ローディングの表示
loading propを使用することで、フォームの読み込み中にローディングを表示させることができます。<template>
<spearly-form id="FORM_ID" loading="Loading" />
</template>フォームのカスタマイズ
フォームslotを使うことで独自にフォームの内容を作ることもできます。なおフォームには、必ず空文字列の _spearly_gotcha を含めてください。
_spearly_gotchaはスパム対策用のフィールドのため、画面上にも見えないフィールドとして以下のコードサンプルのように設置する必要があります。
<template>
<spearly-form id="FORM_ID">
<template v-slot="form"> // フォームのデータを受け取ることができます
<fieldset v-for="field in form.fields" :key="field.identifier">
<label :for="field.identifier">
{{ field.name }}
<i v-if="field.required">*</i>
</label>
<input
:id="field.identifier"
v-model="answers[field.identifier]"
:required="field.required"
type="text"
/>
</fieldset>
...
<input v-model="answers._spearly_gotcha" type="text" style="position: absolute; width: 1px; height: 1px; overflow: hidden;" />
<button @click="form.submit(answers)">Submit</button> // フォームの送信は form.submit を呼び出すことで実行できます
</template>
</spearly-form>
</template>
<script>
export default {
data() {
return {
answers: {
YOUR_FORM_FIELD_ID: '',
...,
_spearly_gotcha: '',
}
}
}
}
</script>nuxt3の場合:<template>
<spearly-form id="FORM_ID">
<template v-slot="form"> // フォームのデータを受け取ることができます
<fieldset v-for="field in form.fields" :key="field.identifier">
<label :for="field.identifier">
{{ field.name }}
<i v-if="field.required">*</i>
</label>
<input
:id="field.identifier"
v-model="state.answers[field.identifier]"
:required="field.required"
type="text"
/>
</fieldset>
...
<input v-model="answers._spearly_gotcha" type="text" style="position: absolute; width: 1px; height: 1px; overflow: hidden;" />
<button @click="form.submit(answers)">Submit</button> // フォームの送信は form.submit を呼び出すことで実行できます
</template>
</spearly-form>
</template>
<script lang="ts" setup>
const state = reactive({
answers: {
YOUR_FORM_FIELD_ID: '',
...,
_spearly_gotcha: '',
},
})
</script>