0
votes

i'm quite new with vue.js i have several function that intended to compile them into one js file. like this:

example.js:

function containObject(obj, list) {
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] === obj) {
      return true;
    }
  }
  return false;
} export default containObject;

function pad(n, width, z) {
  let x = '';
  let p = '';
  x = z || '0';
  p = n.toString();
  return p.length >= width ? p : new Array(width - p.length + 1).join(x) + p;
} export default  pad;

and import them in vue file like this:

example.vue:

import { containObject, pad } from './example';

but there was return error where cannot have two export default in one js file. how can icompile them into one file so that it can be used globally and no need to create the same function every file?

1
Don't use the default keyword. Just do export function ..., that's it. Then your import code would workEric Guan
It has nothing to do with vue.js, it is just a javascriptKingGary

1 Answers

3
votes

In example.js file you can simply export those functions like:

function containObject(obj, list) {
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] === obj) {
      return true;
    }
  }
  return false;
}

function pad(n, width, z) {
  let x = '';
  let p = '';
  x = z || '0';
  p = n.toString();
  return p.length >= width ? p : new Array(width - p.length + 1).join(x) + p;
} 
export {
  containObject,
  pad
}

It will expose those functions which you can import in example.vue file and destructure them.