为什么我的模板计数按钮不刷新页面?

huangapple go评论148阅读模式
英文:

Why My Stencil count button doesn't refresh the page?

问题

I'm new in Stencil.js, but I have experience with React. My first exercise in Stencil doesn't work. I tried to make a simple count button. The result is that:
enter image description here

The var contador is NaN and the number 0 is always 0.

Code Component:

import { Component, h, State } from '@stencil/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
 
  @State() contador = 0;

  contar(){
      this.contador++
      console.log(this.contador);
    }

  render() {
    return (
      <div>
        <button onClick={this.contar}> <span class="menu-icon">CONTAR</span></button>
        <nav class="navigation">
          <ul>
            <li><a href="#" class="logo">{this.contador}</a></li>
          </ul>
        </nav>
      </div>
    );
  }
}

Index.html code

<!DOCTYPE html>
<html dir="ltr" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" />
    <title>Stencil Component Starter</title>

    <script type="module" src="/build/cosmoprueba.esm.js"></script>
    <script nomodule src="/build/cosmoprueba.js"></script>
  </head>
  <body>
    <my-component></my-component>
  </body>
</html>

I was watching this video as a walkthrough:
https://www.youtube.com/watch?v=jm45n5bh2Ak&t=2s

The body doesn't "refresh", it remains at 0 always. I tried another exercises, for example, hiding a text with a button, the result is the same, don't refresh.

I have the last version of node and npm:
npm: '9.5.0',
node: '18.14.2',

英文:

I'm new in Stencil.js, but I have experience with React. My first ejercise in Stencil doesn't work. I tried to make a simple count buttom. The result is that:
enter image description here

The var contador is NaN and the number 0 is always 0.

Code Component:

import { Component, h, State } from &#39;@stencil/core&#39;;

@Component({
  tag: &#39;my-component&#39;,
  styleUrl: &#39;my-component.css&#39;,
  shadow: true,
})
export class MyComponent {
 

  
  @State() contador = 0;


  contar(){
      this.contador++
      console.log (this.contador);
    }


  render() {
    return (
      &lt;div&gt;
        &lt;button onClick={this.contar}&gt; &lt;span class=&quot;menu-icon&quot;&gt;CONTAR&lt;/span&gt;&lt;/button&gt;
          &lt;nav class=&quot;navigation&quot;&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;logo&quot;&gt;{this.contador}&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/nav&gt;
      &lt;/div&gt;
    );
  }
}

Index.html code

&lt;!DOCTYPE html&gt;
&lt;html dir=&quot;ltr&quot; lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0&quot; /&gt;
    &lt;title&gt;Stencil Component Starter&lt;/title&gt;

    &lt;script type=&quot;module&quot; src=&quot;/build/cosmoprueba.esm.js&quot;&gt;&lt;/script&gt;
    &lt;script nomodule src=&quot;/build/cosmoprueba.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;my-component&gt;&lt;/my-component&gt;
  &lt;/body&gt;
&lt;/html&gt;

I was watch this video as walkthrough
https://www.youtube.com/watch?v=jm45n5bh2Ak&amp;t=2s

The body doesn't "refresh", it remains at 0 always. I tried another exercises, for example, hidding a text with a button, the result is the same, don't refresh.

I have the last version of node and npm:
npm: '9.5.0',
node: '18.14.2',

答案1

得分: 0

点击事件处理程序会自动将 this 绑定到触发事件的元素。为了避免这种隐式的绑定,您可以切换到箭头函数,它将避免将绑定到元素对象,并保持对组件实例的引用。

尝试使用箭头函数:

  contar = () => {
      this.contador++
      console.log(this.contador);
    }
英文:

The click event handler automatically binds this to the element triggering the event. In order to avoid this implicing this binding, you can switch to an arrow function which will avoid the binding to the element object and keep a reference to the Component instance.

Try using an arrow function:

  contar = ()=&gt;{
      this.contador++
      console.log (this.contador);
    }

huangapple
  • 本文由 发表于 2023年3月4日 08:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定