简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM