【发布时间】:2021-09-27 09:54:59
【问题描述】:
我有一个 nextjs 应用程序,它通过 api 显示电影及其海报,我试图在所有图像完全加载显示之前为它们添加模糊效果。我在 github 讨论中找到了这个 comment,它使用了 plaiceholder,但我找不到让它适用于 multiple 图像的方法。这是我的一些代码
// index.js
...
return (
<>
<MovieList data={props.data.trending} title="Trending Right Now" />
<MovieList data={props.data.popular} title="Popular" />
</>
);
export async function getStaticProps() {
var promises = [];
const api_key = '...'; // don't worry about api key I can make another one
const urls = [
`https://api.themoviedb.org/3/trending/movie/week?api_key=${api_key}`,
`https://api.themoviedb.org/3/movie/popular?api_key=${api_key}&language=en-US&page=1`
];
urls.forEach(function(url) {
promises.push(
axios.get(url).then(
function(data) {
return { success: true, data: data };
},
function() {
return { success: false };
}
)
);
});
const [trendingRes, popularRes] = await Promise.all(promises);
const trending = trendingRes.success === false ? null : trendingRes.data.data;
const popular = popularRes.success === false ? null : popularRes.data.data;
return { props: { data: { trending, popular } } };
}
// MovieList.js
{props.data
? props.data.results.map((movie, index) => {
return (
<>
...
<div>
<Image
src={`https://image.tmdb.org/t/p/w500${movie.poster_path}`}
alt="movie poster"
width="260"
height="391"
placeholder="blur" // doesn't work
/>
</div>
</>
);
})
: 'Not found'}
你可以看到完整代码here
【问题讨论】:
-
使用动态图片时必须提供
blurDataURLprop。
标签: next.js nextjs-image