[Vuejs]-How to export a function

3👍

Your file extension is wrong. It should be .js.
Secondly, you are writing a composable and not a component. So the directory should be named composables not components.

2👍

in vue js, components are consists of three parts:

<template></template>

<style></style>

<script></script>

all your code must be inside this parts.
your file is .vue format, and your code is out of the mentions parts.
put your code inside the script tag, or change file extension to .js to resolve this problem.

more information


edited:

src/utils/GISWSCaller.is

let res = undefined

export function useGISWSCaller() {
    async function fetchURL(params) {
        console.log('params:', params);

        const splitted = params.split(',')
        if (splitted.length == 4) {
            const response = await fetch('https://xx/xx/getDistanceFromOriginToDestination/' + params)
            res = await response.json()
            return res
        }
        return false
    }

    const ctx = {
        fetchURL
    }

    return ctx
}

src/main.js

import {createApp} from 'vue'
import {useGISWSCaller} from 'GISWSCaller'

const app = createApp({})
const ctx = useGISWSCaller()
app.provide('ctx', ctx)
app.mount('#app');

Leave a comment