Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unresponsive counter component #106

Closed
charlene1804 opened this issue Oct 4, 2021 · 2 comments
Closed

Unresponsive counter component #106

charlene1804 opened this issue Oct 4, 2021 · 2 comments

Comments

@charlene1804
Copy link

charlene1804 commented Oct 4, 2021

Hello,
I would like to get some help understanding why my counter component is unresponsive.

You can see the source code here:

import { Slim } from 'slim-js';

const template = /*html*/`
  <style>
    * {
      font-size: 200%;
    }

    span {
      width: 4rem;
      display: inline-block;
      text-align: center;
    }

    button {
      width: 4rem;
      height: 4rem;
      border: none;
      border-radius: 10px;
      background-color: seagreen;
      color: white;
    }
  </style >
  <button @click="this.dec">-</button>
  <span>{{this.count}}</span>
  <button @click="this.count++">+</button>
`;

class MyCounter extends Slim {
  constructor() {
      super();
      this.count = 0;
  }

  inc() {
    this.count++;
  }

  dec() {
    console.log('dec');
    this.count--;
  }
}

Slim.element(
  'my-counter',
  template,
  MyCounter
);

Thanks in advance.
Charlene

@eavichay
Copy link
Member

eavichay commented Nov 3, 2021

Hi.

Simple changes would make it work...

  • Add import 'slim-js/directives'; to support events. If you wish to only add the event directive, select it specifically.
  • Replace the buttons click behavior to this: <button @click="this.inc()"> instead of this.inc.

Hope this helps.

Full code below.

Closing the issue, please re-open if you have more questions/problems.

import { Slim } from 'slim-js';
import 'slim-js/directives';

const template = /*html*/`
  <style>
    * {
      font-size: 200%;
    }

    span {
      width: 4rem;
      display: inline-block;
      text-align: center;
    }

    button {
      width: 4rem;
      height: 4rem;
      border: none;
      border-radius: 10px;
      background-color: seagreen;
      color: white;
    }
  </style >
  <button @click="this.dec()">-</button>
  <span>{{this.count}}</span>
  <button @click="this.inc()">+</button>
`;

class MyCounter extends Slim {
  constructor() {
      super();
      this.count = 0;
  }

  inc() {
    this.count++;
  }

  dec() {
    console.log('dec');
    this.count--;
  }
}

Slim.element(
  'my-counter',
  template,
  MyCounter
);

@eavichay eavichay closed this as completed Nov 3, 2021
@charlene1804
Copy link
Author

Thank you very much for your answer.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants