全新 Composition API
透過從 @nuxtjs/composition-api
遷移到與 Nuxt 3 相容的 API,在遷移到 Nuxt 3 時可以減少重寫的程式碼。
ssrRef
和 shallowSsrRef
這兩個函式已被新的 composable 取代,該 composable 在底層的運作方式非常相似:useState
。
主要的差異在於您必須為此狀態提供一個key(Nuxt 為 ssrRef
和 shallowSsrRef
自動產生),並且它只能在 Nuxt 3 外掛程式(由 defineNuxtPlugin
定義)或元件實例中呼叫。(換句話說,您不能在全域/環境上下文中使用 useState
,因為跨請求共享狀態存在危險。)
- import { ssrRef } from '@nuxtjs/composition-api'
- const ref1 = ssrRef('initialData')
- const ref2 = ssrRef(() => 'factory function')
+ const ref1 = useState('ref1-key', () => 'initialData')
+ const ref2 = useState('ref2-key', () => 'factory function')
// accessing the state
console.log(ref1.value)
由於狀態是鍵控的,因此您可以從多個位置存取相同的狀態,只要您使用相同的 key 即可。
您可以在 Nuxt 3 文件中閱讀更多關於如何使用此 composable 的資訊。
ssrPromise
此函式已被移除,如果您正在使用它,則需要尋找替代的實作方式。如果您有 ssrPromise
的使用案例,請透過討論告知我們。
onGlobalSetup
此函式已被移除,但其使用案例可以透過在 defineNuxtPlugin
中使用 useNuxtApp
或 useState
來滿足。您也可以在版面配置的 setup()
函式中執行任何自訂程式碼。
- import { onGlobalSetup } from '@nuxtjs/composition-api'
- export default () => {
- onGlobalSetup(() => {
+ export default defineNuxtPlugin((nuxtApp) => {
+ nuxtApp.hook('vue:setup', () => {
// ...
})
- }
+ })
useStore
為了存取 Vuex store 實例,您可以使用 useNuxtApp().$store
。
- import { useStore } from '@nuxtjs/composition-api`
+ const { $store } = useNuxtApp()
useContext
和 withContext
您可以使用 useNuxtApp
存取注入的 helpers。
- import { useContext } from '@nuxtjs/composition-api`
+ const { $axios } = useNuxtApp()
useNuxtApp()
也提供了一個名為 nuxt2Context
的 key,其中包含您通常從 Nuxt 2 context 存取的所有相同屬性,但不建議直接使用它,因為它在 Nuxt 3 中將不存在。相反,請查看是否有其他方法可以存取您需要的內容。(如果沒有,請提出功能請求或討論。)wrapProperty
此 helper 函式不再提供,但您可以使用以下程式碼來取代它
const wrapProperty = (property, makeComputed = true) => () => {
const vm = getCurrentInstance().proxy
return makeComputed ? computed(() => vm[property]) : vm[property]
}
useAsync
和 useFetch
這兩個 composable 可以用 useLazyAsyncData
和 useLazyFetch
取代,它們在 Nuxt 3 文件中有說明。就像之前的 @nuxtjs/composition-api
composable 一樣,這些 composable 不會阻止用戶端路由導航(因此名稱中的「lazy」部分)。
useFetch
所做的那樣)。useLazyFetch
必須已針對 Nitro 進行設定。從 useAsync
遷移到新的 composable
<script setup>
- import { useAsync } from '@nuxtjs/composition-api'
- const posts = useAsync(() => $fetch('/api/posts'))
+ const { data: posts } = useLazyAsyncData('posts', () => $fetch('/api/posts'))
+ // or, more simply!
+ const { data: posts } = useLazyFetch('/api/posts')
</script>
從 useFetch
遷移到新的 composable
<script setup>
- import { useFetch } from '@nuxtjs/composition-api'
- const posts = ref([])
- const { fetch } = useFetch(() => { posts.value = await $fetch('/api/posts') })
+ const { data: posts, refresh } = useLazyAsyncData('posts', () => $fetch('/api/posts'))
+ // or, more simply!
+ const { data: posts, refresh } = useLazyFetch('/api/posts')
function updatePosts() {
- return fetch()
+ return refresh()
}
</script>
useMeta
為了與 vue-meta
互動,您可以使用 useNuxt2Meta
,它將在 Nuxt Bridge(但不是 Nuxt 3)中運作,並允許您以與 vue-meta
相容的方式操作您的 meta 標籤。
<script setup>
- import { useMeta } from '@nuxtjs/composition-api'
useNuxt2Meta({
title: 'My Nuxt App',
})
</script>
您還可以傳入 computed 值或 refs,並且 meta 值將會反應式地更新
<script setup>
const title = ref('my title')
useNuxt2Meta({
title,
})
title.value = 'new title'
</script>
useNuxt2Meta()
和 Options API head()
,因為行為可能無法預測。Nuxt Bridge 也提供與 Nuxt 3 相容的 meta 實作,可以使用 useHead
composable 存取。
<script setup>
- import { useMeta } from '@nuxtjs/composition-api'
useHead({
title: 'My Nuxt App',
})
</script>
您還需要明確地在您的 nuxt.config
中啟用它
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
meta: true
}
})
此 useHead
composable 在底層使用 @unhead/vue
(而不是 vue-meta
)來操作您的 <head>
。因此,建議不要同時使用原生 Nuxt 2 head()
屬性和 useHead
,因為它們可能會衝突。
有關如何使用此 composable 的更多資訊,請參閱 Nuxt 3 文件。
顯式匯入
Nuxt 使用 #imports
別名公開每個自動匯入,如果需要,可以使用它來進行顯式匯入
<script setup lang="ts">
import { ref, computed } from '#imports'
const count = ref(1)
const double = computed(() => count.value * 2)
</script>
停用自動匯入
如果您想停用自動匯入 composable 和工具程式,您可以在 nuxt.config
檔案中將 imports.autoImport
設定為 false
。
export default defineNuxtConfig({
imports: {
autoImport: false
}
})
這將完全停用自動匯入,但仍然可以從 #imports
使用顯式匯入。