Utilizing Moment.js fromnow functionality with Vue.js and Laravel framework

Currently utilizing Laravel with Vue for my project

 Vue.http.headers.common['X-CSRF-TOKEN'] = $("#token").attr("value");
        new Vue({
          el: '#notificationMenu',
          data: {
            patients: []
          },
            created: function(){
                this.getPatients();
                this.addUsers();
            },
            methods: {
                getPatients: function(){
                       $.getJSON("{{route('api_patients')}}", function(patients){
                        this.patients = patients;
                    }.bind(this));
                    setTimeout(this.getPatients, 1000); 
                },
                addUsers: function(){
                 // var newItem = {'name':'asdfasdf','email':'sdfsdf','password':'sdfsdf'}
                 // var input = this.newItem;
                  this.$http.get('/govaccine/addUsers', '').then((response) => {
                  }, (response) => {
                    this.formErrors = response.data;
                    });

                }
            }

        });

Here is my Vue component:

   <li class=" notif unread" v-for="patient in patients">
                      <a href="#">
                        <div class="messageblock">
                           <div class="message"><strong>@{{ patient.patient_lname}} @{{ patient.patient_fname}}'s</strong> next vaccination is due on 2017-03-23 for @{{patient.vaccine_name}}
                          </div>
                            <div class="messageinfo">
                            <i class="icon-trophy"></i>2 hours ago
                          </div>
            </div>
         </a>
    </li>

I am trying to display the created at column in my view utilizing the fromnow function of moment.js. I've searched for solutions but haven't found one that works for me.

Upon using required('moment'), I keep encountering the error "require is not defined" even after installing browserify via npm install -g browserify.

Are there any suggestions on how to resolve this issue or an alternative method to retrieve the fromnow time or something similar to carbon's difffromhuman function?

Answer №1

When working within templating, it's important to note that only variables declared in Vue's data can be accessed. Here are your options:

  1. Include a reference to moment in Vue's data.
  2. Create a method in Vue to handle moment calculations.
  3. Use a computed property to handle moment calculations.

Using a computed property is the recommended approach.

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

Upon its second use, Ajax is loaded with cached data

Imagine a table with multiple rows, each row containing a SHOW button that reveals hidden content when clicked. The hidden div (with the classname "Content") loads the content of a second page using ajax and the id of the corresponding table row. However, ...

When populating data, two ID fields (_id and id) are generated as duplicates

Upon retrieving information from my database, I have noticed the presence of an additional id field alongside the standard _id field. These two fields consistently contain identical values. However, it seems that the id field appears only during population ...

The onClick event for a q-btn does not seem to be functioning properly when used

Inside the q-btn, there is a q-checkbox. Additionally, I included a div to style the text. <q-btn flat color="blue" class="full-width no-padding" @click="tog(item)" > <q-checkbox ...

The API fails to update with new data after every request

I have encountered an issue while working on my API project. The API is developed in DRF and the response is being shown on a Vue project. The problem arises when the API needs to refresh after each request (browser refresh). Initially, I display all the ...

What is the best way to ensure that any modifications made to an item in a table are appropriately synced

Utilizing xeditable.js, I am able to dynamically update the content of a cell within a table. My goal is to capture these changes and send them via an HTTP request (PUT) to the backend in order to update the database. Below is the table that can be edited ...

What could be the reason why I am unable to choose my radio button? What error am I

The output can be found here... I am struggling to use a radio button and cannot seem to select it. Can someone please explain what issue I am facing? Any help would be greatly appreciated. const [value, setValue] = React.useState({ yes:"", no:"" ...

JavaScript OOP Function call not functioning in IE9

When using IE9, a JavaScript OOP Function call does not seem to work for me. Here is my code snippet: var newobj = new SAObject(); <input onclick="newobj.function()" /> Upon clicking the button, nothing happens. No alert is displayed, and it seem ...

Ways to extract values from a javascript hash map by exclusively incorporating an array

So here's the issue I'm encountering. Let's consider the following scenario: let surfaces: Map<any, any> = new Map([{"83.1" => Object}, {"84.1" => Object}]) let arr1 = ["83.1"] This is the desired o ...

Designing a versatile pop-up window with jQuery or JavaScript that functions seamlessly across all web browsers

I've encountered an issue with my code where it displays a popup message correctly in Chrome, but when testing it on Firefox and Edge, it creates a strange box at the end of the page instead. Here is the code snippet I'm referring to: var iframe ...

JavaScript event manually triggered not propagating within an element contained in an iFrame context

I am currently developing a WYSIWYG designer that enables users to choose colors through [input type="color"] fields. The interface includes inputs on the left side and an iFrame on the right side displaying the generated preview. Normally, when ...

The issue of Angular 2.3.1 custom validator promise not resolving

I am currently working on implementing a sign up form in which I need to check if the username is already taken or not. To accomplish this, I have decided to use promises. The structure of my sign-up component is as follows: import { Component, OnInit } f ...

Exporting all components using require: A complete guide

Currently, I am working on a Vue js application where I am tasked with exporting all the files and components from a specific directory. These files/components need to be imported into a separate file named paths.js. If I use the require function to impor ...

Retrieving the value of an inputtext component in PrimeFaces using jQuery

I have the following code snippet: <h:form id="calcForm"> <h:panelGrid columns="3" styleClass="panelGridNoBorder"> <p:inputText id="value" /> <p:commandButton value="calculate " onclick="calc()" /> </h:panelGrid> ...

Strange behavior observed when creating object using spread operator

Utilizing Vue.js with a spread operator to generate a new array and locate a specific value within it that will then be assigned to the selectedEmployee data property: selectEmployee(id) { const employees = [...this.employees]; this.selectedEmployee = ...

Accessing unique entity information in Shopware's CMS editor through a component

While working on a Shopware vue component for the editor/designer in the administration, I encountered this code snippet to fetch data from a custom entity: fetchTeam({ targetId }) { this.teamRepository.get(targetId).then((team) => { this.$set(thi ...

Utilize different versions of jQuery along with various plugins

I have integrated the AdminLTE Template into my project, which requires jQuery Version 3.X. Additionally, I am using the Flotchart jQuery library, which specifically needs jQuery Version 1.11.3. To handle this conflict, I have implemented the $.noConflic ...

Manipulating DropDownList Attributes in ASP.NET using JavaScript

I am facing an issue with populating a Dropdownlist control on my ASCX page. <asp:DropDownList ID="demoddl" runat="server" onchange="apply(this.options[this.selectedIndex].value,event)" onclick="borderColorChange(this.id, 'Click')" onblur="bo ...

What is the best way to establish and maintain lasting connections with the Firebase database while utilizing the superagent

Currently, I am following the Firebase Functions documentation on enhancing Firebase database performance. I have provided the code snippet below for your reference. const request = require('superagent'); const functions = require('fireba ...

Is there a way to retrieve all the selected values from multiple select inputs when a button is clicked in React?

I am currently working on developing a questionnaire where the select inputs' values are collected and averaged to generate a final score. Although it may sound simple, I'm facing difficulties in retrieving these values. At this point, my primary ...

Comment sections that refresh automatically after being posted

I am determined to provide a clear explanation. Despite having some code, I am uncertain about how to make it clone comments and add new ones with user inputted text. Below is the snippet of code I am struggling with: <!DOCTYPE html> <!-- this i ...