Michael Paccione
Michael Paccione

Reputation: 2827

Can't import functions into Vue.js Single File Component

I have several vue's which require custom date functions. I am attempting to create a date_module.

datemodule.js

export default {
    getNewDate(offset){
        let newDate = this.getDate();
        newDate.setDate(newDate.getDate() + offset);
        return newDate;
    },
    getDate(){
        let date = new Date();
        return date;
    },
    ...etc
}

I am attempting to import it and call it in my components as DM.getDate() etc.

DailySchedule.vue

...
import DM from "../datemodule.js"
...

This doesn't seem to work?

Upvotes: 3

Views: 155

Answers (2)

Mohammad Imani
Mohammad Imani

Reputation: 547

here i had a sample and this worked for me.

dateUtils.js

import moment from "moment";

export const convertDate = (date) => {
  return moment(date).format("DD MMMM YYYY");
};

Then to use Convertdate function like :

import {convertDate} from "./dateUtils.js";


setup(){
   convertDate("date")
}

Upvotes: 0

akuiper
akuiper

Reputation: 215117

Try refactor datemodule.js:

function getNewDate(offset){
  let newDate = this.getDate();
  newDate.setDate(newDate.getDate() + offset)        
  return newDate;
}

function getDate(){
  let date = new Date();
  return date
}

export default { getNewDate, getDate }

And then import DM from '../datemodule.js'. Notice you are writing normal javascript modules here instead of a Vue component.

Upvotes: 2

Related Questions