Implementing Vue.js click event behavior on a component within the template

Encountering an issue while developing Vue's template onclick event. Trying to open a module when clicking a file, I have looked at some samples with native code, but it does not quite fit into my code and I cannot reach the console. Here is my code:

Added native but still no response in the console log.

HTML

<div id="app">
<notification :notice="notice" ></notification>
</div>

JavaScript

const myTest = [ 
  {
    title: 'mamam',
    body: 'Mamamia',
    type: 'success'
  },{
    title: 'Perlu',
    body: 'Hati-hati sedang ada perbaikan',
    type: 'Danger'
  },{
    title: 'Notification',
    body: 'Repellendus quod aliquid ad, quae harum similique facilis aliquam. Dolorum deserunt alias officia atque dolor, voluptas harum. Inventore reiciendis reprehenderit provident!',
    type: 'primary'
  } 
];

function randomInt(min, max) {
  let rand = min - 0.5 + Math.random() * (max - min + 1);
  rand = Math.round(rand);
  return rand;
};

Vue.component('notification', {
  template: 
  `
  <div class="Notification" v-show="notice">
    <div class="notice" :class="notice.type">
      <svg @click="notice=!notice" class="close" width="20" height="20" viewbox="0 0 20 20" stroke-width="2">
        <line x1="3" y1="3" x2="17" y2="17"></line>
        <line x1="3" y1="17" x2="17" y2="3"></line>
      </svg>
      <div class="title">
        <i class="type" :class="notice.type">{{notice.type}}</i>
        {{notice.title}}
      </div>
      <div class="body">
        <i class="type" :class="notice.type">{{notice.body}}</i>
        <a @click.native="showFile"> File </a>
      </div>
    </div>
  </div>
  `,
  props: {
     notice: {
      type: [Object, Array]
    },
  },
});

setInterval(() => ( vm.$data.index = randomInt(0, 6) ), randomInt(100, 10000) );

const vm = new Vue({
  el: "#app",
  data: {
    index: 0
  },
  computed: {
    notice() {return myTest[this.index]}
  },
  methods: {
        showFile(e) {
          console.log(' show file was clicked. ')
      }
}
});

Answer №1

It seems that there are a few issues in the code provided:

  1. showFile needs to be defined within the notification component as it cannot access parent showFile methods

Vue.component('notification', {
      template: 
      `
      <div class="Notification" v-show="notice">
        <div class="notice" :class="notice.type">
          <svg @click="notice=!notice" class="close" width="20" height="20" viewbox="0 0 20 20" stroke-width="2">
            <line x1="3" y1="3" x2="17" y2="17"></line>
            <line x1="3" y1="17" x2="17" y2="3"></line>
          </svg>
          <div class="title">
            <i class="type" :class="notice.type">{{notice.type}}</i>
            {{notice.title}}
          </div>
          <div class="body">
            <i class="type" :class="notice.type">{{notice.body}}</i>
            <a @click.native="showFile"> File </a>
          </div>
        </div>
      </div>
      `,
      props: {
         notice: {
          type: [Object, Array]
        },
      },
      methods: {
        showFile(e) {
          console.log(' show file was clicked. ')
        }
      }
    });

  1. It is advisable not to directly modify the notice props within the children component (Further details)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing AJAX requests in jQuery DataTable with ASP.NET MVC

For some time now, I have been using the jQuery DataTables 1.10.13 plugin. Recently, I encountered an issue related to the ajax data source for my HTML table. This is how I initialized jQuery DataTable inside Files.cshtml <script language="javascript" ...

Leveraging IE conditional comments for including CSS or JavaScript files can lead to an increase in the number of HTTP

Our web designer has implemented special pages for Internet Explorer by using IE-specific comments. This means that certain stylesheets are only loaded if the user is using a specific version of IE: <!--[if lt IE 7]> <link type="text/css" rel="st ...

Unable to launch Vue application in a production environment

Recently, I attempted to launch a Vue application in a production environment. Following the execution of the "npm run build" command, I proceeded to transfer both the "index.html" and "dist" files into my apache root directory. However, upon attempting to ...

In the realm of numeric input in JavaScript (using jQuery), it is interesting to note that the keyCode values for '3' and '#' are identical

I am in need of setting up an <input type="text" /> that will only accept numeric characters, backspace, delete, enter, tabs, and arrows. There are many examples out there, and I started with something similar to this: function isNumericKeyCode (ke ...

What are the best ways to engage with a div element using keyboard shortcuts?

Is it possible to enable keyboard shortcuts for interacting with div elements? I am working on a project tailored for seniors who may have difficulty using a mouse. Is there a way to utilize keyboard shortcuts to click on divs and access their contents? H ...

Differences in disabled option value selection between jQuery legacy and updated versions

Recently, I upgraded the jQuery version in my project from 1.7.2 to 2.1.1 and included jquery migrate for fallback support. During testing, I encountered an issue with the value of a select element in jQuery versions 2.1.1 and 1.7.2: For version 1.7.2, t ...

Guide to navigating to a particular component in React JS

I've designed a web application using React that doesn't contain any HTML, and I've broken down each page into smaller modules. For instance, on the main page, the first module (located at the top) contains a button that, when clicked, shoul ...

Utilizing JavaScript's Array.sort() method for sorting objects with varying properties

Currently, I am dealing with these specific Objects: let obj1 = { from: Date, to: Date } let obj2 = { date: Date } These Objects have been placed in an Array: let arr = [ obj1, obj2 ] My goal is to organize the objects within the array by using arr.sort( ...

Place the child DIV at the bottom of its parent DIV

I've searched through open questions but couldn't find a solution. The code snippet in question looks like this: ... <div style="some height"> <div style="some width"> .......and so on.. .. < ...

Tips for displaying specific HTML elements in AngularJS using ng-repeat and ng-if

I am working with some bootstrap HTML code that includes an ng-repeat function. <div class="row"> <div class="col-lg-4" ng-repeat="p in persons"> <img class="img-circle" src="images/{{ p.image }}" width="140" height="140"> < ...

What is a superior option to converting to a promise?

Imagine I am creating a function like the one below: async function foo(axe: Axe): Promise<Sword> { // ... } This function is designed to be utilized in this manner: async function bar() { // acquire an axe somehow ... const sword = await foo ...

Activate lighting in Three.js with a simple click

I successfully loaded a sphere mesh with Lambert material. Now, I am trying to add a light source to the point where there is an intersection after clicking. target = object that was clicked. to = vector3 of my click. When the dblclick event listener is ...

Implement Offcanvas feature with Bootstrap 3 Navbar set to Fixed Top position

I am in the process of creating a website that will feature a large menu. However, I am not a fan of the collapsed menu that comes with BS3. Instead, I would like to implement a drawer or off-canvas menu similar to the one showcased in BS3's offcanvas ...

Adding ids dynamically to href elements in Vue.js

As a newcomer, I am facing an issue with dynamically adding an id to my href tag. Below is the code snippet: <div class="col-md-6" v-for="Product in data"> <div class="panel panel-default" > <div class="panel-heading"> ...

Send dropdown selections to a Javascript function and convert them into an array

Within my JavaScript function, I need to pass multiple selected values from dropdown menus and store them in a JavaScript array. Each time a value is selected and sent using the onchange() function, it should be added to the array. If the same value is sel ...

Incorporating a dropdown menu into an HTML table through jQuery proves to be a

I loaded my tabular data from the server using ajax with json. I aimed to generate HTML table rows dynamically using jQuery, with each row containing elements like input type='text' and select dropdowns. While I could create textboxes in columns ...

Vue JS - Issue with rendering <tr> and <td> elements in table component

I have created a custom table component using vue.js: <template> <div> <table class="table border"> <thead> <tr> <td>header 1&l ...

Cypress: harnessing the power of regular expressions within jQuery selectors for the ":contains()" method

Trying to use Cypress along with a regular expression to target an element that includes specific text. The following get() function successfully works: cy.get('[data-cy=tile]').contains(new RegExp(myVar)) However, the following command does no ...

Stop CSS tooltip from overflowing outside of the page or window

One issue I am facing is with a CSS-only tooltip that uses a span to display the tooltip when hovering over a link. The problem arises when the link is positioned near the top or side of a page, causing the tooltip to extend beyond the edge of the page. I ...

Stop the form from being submitted using ajax if there are no values in the form fields

Having issues with a basic form. Struggling to prevent submission when fields are empty. Is there a straightforward way to validate the fields and stop the form from submitting? Below is the HTML form: <form method="post" name="simpleForm" id="simpleF ...