我想为此页面渲染一个背景图像,不是整个应用程序

发布于 2025-02-10 05:27:04 字数 1848 浏览 2 评论 0原文

这是我的登录页面代码,但我无法为此页面设置BG映像,我可以通过将其设置为应用程序组件,如果我对此进行相同的操作,则将其设置为整个应用程序我通过导入图像来完成的整个页面

import React, { useState } from "react";
import "./Loginp.css";
import axios from "axios";

import bgimg from "../../assets/Loginimage.jpg";

function Loginp() {
  const [usernamed, setusername] = useState("");
  const [passwordd, setpassword] = useState("");

  const check = false;

  function clicki() {
    console.log("clicked");
    const dat = {
      username: usernamed,
      password: passwordd,
    };
    axios.post("http://localhost:7000/login", dat).then((resp) => {
      check = resp.data;
    });
    if (check) {
      alert("User name already exists");
    }
  }
  return (
    <div
      id="loginform"
      style={{
        backgroundImage: bgimg,
        height: 100,
        marginTop: 10,
        textAlign: "center",
      }}
    >
      <FormHeader title="Login" />
      {/* <Form /> */}
      <div>
        <div class="row">
          <label>Username</label>
          <input
            type="text"
            placeholder="Enter your username"
            onChange={(e) => {
              setusername(e.target.value);
            }}
          />
        </div>

        <div class="row">
          <label>Password</label>
          <input
            type="text"
            placeholder="Enter your password"
            onChange={(e) => {
              setpassword(e.target.value);
            }}
          />
        </div>
        <div id="button" class="row">
          <button onClick={clicki}>Log in</button>
        </div>
      </div>
    </div>
  );
}

const FormHeader = (props) => <h2 id="headerTitle">{props.title}</h2>;

export default Loginp;

this is my code for login page but i am not able to set bg image for this page i can do that by setting to app component which sets to whole application if i do the same to this then only the component is getting the background image not the whole page i am doing it by importing image

import React, { useState } from "react";
import "./Loginp.css";
import axios from "axios";

import bgimg from "../../assets/Loginimage.jpg";

function Loginp() {
  const [usernamed, setusername] = useState("");
  const [passwordd, setpassword] = useState("");

  const check = false;

  function clicki() {
    console.log("clicked");
    const dat = {
      username: usernamed,
      password: passwordd,
    };
    axios.post("http://localhost:7000/login", dat).then((resp) => {
      check = resp.data;
    });
    if (check) {
      alert("User name already exists");
    }
  }
  return (
    <div
      id="loginform"
      style={{
        backgroundImage: bgimg,
        height: 100,
        marginTop: 10,
        textAlign: "center",
      }}
    >
      <FormHeader title="Login" />
      {/* <Form /> */}
      <div>
        <div class="row">
          <label>Username</label>
          <input
            type="text"
            placeholder="Enter your username"
            onChange={(e) => {
              setusername(e.target.value);
            }}
          />
        </div>

        <div class="row">
          <label>Password</label>
          <input
            type="text"
            placeholder="Enter your password"
            onChange={(e) => {
              setpassword(e.target.value);
            }}
          />
        </div>
        <div id="button" class="row">
          <button onClick={clicki}>Log in</button>
        </div>
      </div>
    </div>
  );
}

const FormHeader = (props) => <h2 id="headerTitle">{props.title}</h2>;

export default Loginp;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2025-02-17 05:27:04
import React from 'react';
import backgroundImage from './your-image-path.jpg';

function YourComponent() {
  const style = {
    backgroundImage: `url(${backgroundImage})`,
    backgroundRepeat: 'no-repeat',
    backgroundSize: 'cover',
  };

  return (
    <div style={style}>
      {/* your page content goes here */}
    </div>
  );
}

export default YourComponent;
import React from 'react';
import backgroundImage from './your-image-path.jpg';

function YourComponent() {
  const style = {
    backgroundImage: `url(${backgroundImage})`,
    backgroundRepeat: 'no-repeat',
    backgroundSize: 'cover',
  };

  return (
    <div style={style}>
      {/* your page content goes here */}
    </div>
  );
}

export default YourComponent;
旧梦荧光笔 2025-02-17 05:27:04

尝试一下。

背景图:url($ {bgimg})

try this.

backgroundImage: url(${bgimg})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文