Kathiravan K
Kathiravan K

Reputation: 1

Explicitly Import only one function from exported constant which stores multiple functions

I've a Constant like below stored in a file helper.js

export const Helper = {
  a() {
    return "This is just a string"
  },
  convertToSlug(str) {
    return str
      .toLowerCase()
      .replace(/[^\w ]+/g, "")
      .replace(/ +/g, "-")
  }
}

From this i want to import only convertToSlug function in another file like below

import { convertToSlug } from "helper.js"

How to do that? I tried the same but getting error

"export 'convertToSlug' was not found in '@/utils/helper'

Upvotes: 0

Views: 279

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161657

If you want import { convertToSlug } from "helper.js" to work then you need to have an export named convertToSlug. Right now you've grouped everything inside Helper but there doesn't seem to be any reason for that. It looks like most likely you'd want

export function a() {
  return "This is just a string"
}
export function convertToSlug(str) {
  return str
    .toLowerCase()
    .replace(/[^\w ]+/g, "")
    .replace(/ +/g, "-")
}

Upvotes: 1

Related Questions