refresh
refresh
允许您从服务器操作中刷新客户端路由器。
用法
refresh
只能从服务器操作中调用。它不能在路由处理程序、客户端组件或任何其他上下文中使用。
参数
refresh(): void;
返回
refresh
不返回任何值。
示例
app/actions.ts
'use server'
import { refresh } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPost(formData: FormData) {
const title = formData.get('title')
const content = formData.get('content')
// Create the post in your database
const post = await db.post.create({
data: { title, content },
})
refresh()
}
在服务器操作之外使用时出错
app/api/posts/route.ts
import { refresh } from 'next/cache'
export async function POST() {
// This will throw an error
refresh()
}
这有帮助吗?