typescript TypeError:(0,next_sanity__WEBPACK_IMPORTED_MODULE_0__.createImageUrlBuilder)不是函数

pobjuy32  于 2023-05-19  发布在  TypeScript
关注(0)|答案(5)|浏览(274)

我试图使nextjs typescript项目使用sanity.io,但我无法解决这个问题。这表明createImageUrlBuilder不是一个函数。
enter image description here
here is my sanity module

bq3bfh9z

bq3bfh9z1#

createImageUrlBuilder不再被next-sanity Package ,你需要自己安装依赖项(https://github.com/sanity-io/next-sanity#createimageurlbuilder-is-removed)

$ npm install @sanity/image-url
// or
$ yarn add @sanity/image-url

还请注意,createImageUrlBuilder现在是默认导入

-import { createImageUrlBuilder } from 'next-sanity'
+import createImageUrlBuilder from '@sanity/image-url'
iugsix8n

iugsix8n2#

您需要在createImageUrlBuilder中传递sanity客户端而不是config。

import createClient from '@sanity/client';
import createImageUrlBuilder from '@sanity/image-url';

const config = sanityClient({
   projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID, 
   
   ...
  });

export const  sanityClient = createClient(config);

export const urlFor = (source) => createImageUrlBuilder(client).image(source); // <-----------
lsmepo6l

lsmepo6l3#

首先运行此命令

npm install --save @sanity/image-url

导入createImageUrlBuilder

import createImageUrlBuilder from "@sanity/image-url";

我就是这样。

hk8txs48

hk8txs484#

首先安装@sanity/image-url

$ npm install @sanity/image-url 
//or 
$ yarn add @sanity/image-url

import imageUrlBuilder from "@sanity/image-url";
export const urlFor = (source) => imageUrlBuilder(config).image(source);
使用imageUrlBuilder代替createImageUrlBuilder。它为我工作,谢谢!!

w8biq8rn

w8biq8rn5#

当你想在你的组件中导入“urlFor”时,确保你像这样导入...

import { urlFor } from "../lib/sanity";

相关问题