简体   繁体   English

类型上不存在属性“initGradient”

[英]Property 'initGradient' does not exist on type

I am building a website with react and typescript.我正在用反应和打字稿建立一个网站。 I am having trouble implementing a gradient background that I found on -"https://kevinhufnagl.com/how-to-stripe-website-gradient-effect/".我无法实现在“https://kevinhufnagl.com/how-to-stripe-website-gradient-effect/”上找到的渐变背景。 When I initialise the gradient, I am getting an error " Property 'initGradient' does not exist on type 'Gradient' " The error is raised at 'initGradient' in the code below当我初始化渐变时,我收到一个错误“ Property 'initGradient' does not exist on type 'Gradient' ” 下面代码中的 'initGradient' 引发了错误

This is the code I have written: `这是我写的代码:`

function App() {
  <script src="./Gradient"></script>;
  const gradient = new Gradient();
  gradient.initGradient("#gradient-canvas");
  return (

    <div>
      <canvas
        id="gradient-canvas"
        className="absolute w-50 h-50"
        ></canvas>
    </div>
  );
}

export default App;`

I have created a file 'Gradient.js' which has the code taken from the website linked above.我创建了一个文件“Gradient.js”,其中的代码取自上面链接的网站。 I have also implemented the necessary styling in App.css我还在 App.css 中实现了必要的样式

I have used the same code to create a website using standard HTML and CSS and was able to implement it correctly.我使用相同的代码使用标准 HTML 和 CSS 创建了一个网站,并且能够正确实现它。 However, I am now trying to build the site in React and Typescript.但是,我现在正尝试在 React 和 Typescript 中构建站点。 I have also used tailwind in this project.我也在这个项目中使用了顺风。 Any help and advice would be greatly appreciated cause I've spent way too long trying to implement this!任何帮助和建议将不胜感激,因为我花了太长时间试图实现这一点! Thanks!谢谢!

It looks like you are trying to import the Gradient.js file by using a script tag?您似乎正在尝试使用脚本标记导入 Gradient.js 文件? If thats the case, you should use a module import.如果是这样,您应该使用模块导入。

import gradient from './Gradient'

function App() {
  gradient.initGradient("#gradient-canvas");

  return (

    <div>
      <canvas
        id="gradient-canvas"
        className="absolute w-50 h-50"
        ></canvas>
    </div>
  );
}

export default App;`

As for the gradient.initGradient("#gradient-canvas");至于gradient.initGradient("#gradient-canvas"); I would have to see the actual Gradient.js file to see if that would work.我必须查看实际的 Gradient.js 文件,看看它是否可行。

I spent about 6 hours today using various mesh gradient implementations with React before I ended up with this我今天花了大约 6 个小时在 React 中使用各种网格渐变实现,然后才完成这个

Gradient component:渐变成分:

 useEffect(() => {
        setTimeout(() => {
            const gradient: any = new Gradient();
            gradient.initGradient("#gradient-canvas");
        }, 1);
    }, []);
    return <canvas id="gradient-canvas" data-transition-in />;

CSS CSS

canvas {
    width: 100%;
    height: 100%;
    --gradient-color-1: #c3e4ff;
    --gradient-color-2: #6ec3f4;
    --gradient-color-3: #eae2ff;
    --gradient-color-4: #b9beff;
}

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

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