简体   繁体   English

如何将图像导入 Next.js 组件并渲染它?

[英]How to import images into a Next.js component and render it?

I have a React component which works perfectly in an App.js file of react, I am making a page with Next.js and I want to implement this code but I have problems converting it from react to a Next.js project.我有一个 React 组件,它在 React 的 App.js 文件中完美运行,我正在用 Next.js 创建一个页面,我想实现这段代码,但是我在将它从 React 转换为 Next.js 项目时遇到了问题。 I tried several ways but the image is not rendered or does not appear.我尝试了几种方法,但图像没有渲染或没有出现。

React Code - App.js反应代码 - App.js

import React, { Component } from 'react';
import './App.css';
import image from './images/1.png';
import {Curtains} from 'curtainsjs';
const planeParams = {
  vertexShaderID: "plane-vs", // our vertex shader ID
  fragmentShaderID: "plane-fs", // our framgent shader ID
  uniforms: {
    time: {
      name: "uTime", // uniform name that will be passed to our shaders
      type: "1f", // this means our uniform is a float
      value: 0,
    },
  }
};
class CurtainsPage extends React.Component {
  constructor( props ) {
    super(props);
    this._planes = null;
  }

  componentDidMount() {
    // if we got our curtains object, create the plane
    this.props.curtains && this.createPlanes(this.props.curtains);
  }

  componentWillUpdate(nextProps, nextState) {
    // if we haven't got our curtains object before but got it now, create the plane
    if(!this.props.curtains && nextProps.curtains) {
      this.createPlanes(nextProps.curtains);
    }
  }

  componentWillUnmount() {
    // remove the plane before unmounting component
    if(this.props.curtains && this._planes) {
      this.props.curtains.removePlane(this._planes);
      this._planes = null;
    }
  }
  createPlanes(curtains) {
    // create our plane
    if(curtains) {
      this._planes = curtains.addPlane(this.planeElement, planeParams);
      this._planes.onRender(function() {
        this.uniforms.time.value++;
      });

    }
  }
// register our plane element ref
  registerPlaneElement(el) {
    this.planeElement = el;
  }

  render() {

    return (
        <div
            className="curtain"
            ref={(el) => this.registerPlaneElement(el)}
        >
          <img src={image} alt={"image"}/>
        </div>
    );
  }
}
class App extends Component {

  constructor( props ) {
    super(props);

    this.state = {
      curtains: null
    };
  }

  componentDidMount() {
    let curtains = new Curtains("canvas");
    this.setState({ curtains: curtains });
  }

  render() {
    let curtains = this.state.curtains;
    console.log("start",curtains)
    return (
              <div className="App">
                    <div id="canvas" />
                    <CurtainsPage curtains={curtains}/>
              </div>
    );
  }
}

export default App;

Where i need to implement it.我需要在哪里实施它。

Next JS Code - Index.js下一个 JS 代码 - Index.js

import { Fragment, Component } from "react";
import Nav from "../components/Nav";
import "./index.scss";

class Index extends Component {
  constructor(props) {
    super(props);

    this.state = {
      isLoading: false
    };
  }

  render() {
    return (
      <Fragment>
        <Nav />
        <div id="homeSection">
          <div id="bigHeader">

          </div>
          <div id="headerFooter"></div>
        </div>
      </Fragment>
    );
  }
}

export default Index;

try to use just the file path尝试只使用文件路径

<img src="/static/image.png" alt="image"/>

import Image from "next/image";
function Home() {
  return (
    <>
      <Image src="/me.png" />
    </>
  );
}
export default Home;

https://webpack.js.org/concepts/#loaders https://webpack.js.org/concepts/#loaders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM