create button component with nuxtj, i want define click event in page not component in javascript

To create a button component in Nuxt.js and define the click event in the page instead of the component's JavaScript, you can follow these steps:

  1. Create a new component in the components directory of your Nuxt.js project. Name it MyButton.vue (or any other desired name).

  2. In the MyButton.vue file, define the button markup and emit a custom event when the button is clicked. Here's an example:

<template>
  <button @click="$emit('customClick')">Click Me</button>
</template>

<script>
export default {
  name: "MyButton",
};
</script>
141 chars
10 lines
  1. In the page where you want to use the button component, import the MyButton component and use it accordingly. Define the click event handler on the page itself. Here's an example:
<template>
  <div>
    <my-button @customClick="handleClick"></my-button>
  </div>
</template>

<script>
import MyButton from '@/components/MyButton.vue';

export default {
  components: {
    MyButton,
  },
  methods: {
    handleClick() {
      // Your click event logic here
      console.log("Button clicked!");
    },
  },
};
</script>
341 chars
22 lines
  1. Make sure to import the MyButton component in the script section of the page and use it within the template section.

By following these steps, you can create a button component in Nuxt.js and define the click event within the page's JavaScript rather than within the component itself.

gistlibby LogSnag