如何导出一个函数

huangapple go评论59阅读模式
英文:

How to export a function

问题

The code you provided appears to be in JavaScript or a similar language. Here's the translated content:

如下所示的代码中我想要导出 `GISWSCaller` 函数我正在按照 [此链接][1] 中的教程进行操作并希望创建与该链接中第三个代码段类似的内容
我遇到的问题是对于下面发布的代码VS Code 无法识别该代码为有效代码我的意思是当将下面发布的相同代码粘贴到 VS Code 中时它会变成白色如附带的屏幕截图所示

请告诉我如何正确导出下面发布的代码

**更新后的代码 .vue**:

```javascript
import { provide } from 'vue';
let res = undefined

export default function GISWSCaller() {

  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
  }

  provide('GISWSContext', ctx)
  return fetchURL
}

图像:

如何导出一个函数


This is the translated content from your provided code, and it excludes the code-related content you requested not to translate.

<details>
<summary>英文:</summary>

as shown in the below posted code, i want to export the function `GISWSCaller`. i am following the tutorial in [this link][1], and i want to create something as in the 3rd code snippet in that link.
the problem i am encountring is, for the below posted code, the VS-codes studio can not recognize the code as valid. i mean the same code posted below when pasted into the VS-codes, it becomes colored in whitw as shonw in the screen shot attached.

please let me know how correctly i should export the code posted below.

**updated code .vue**:

    import { provide } from &#39;vue&#39;;
	let res = undefined

	export default function GISWSCaller(){

	  async function fetchURL(params){
		console.log(&quot;params:&quot;,params);
		
		const splitted = params.split(&#39;,&#39;)
		if (splitted.length == 4) {
		  const response = await fetch(&#39;https://xx/xx/getDistanceFromOriginToDestination/&#39; + params)
		  res =  await response.json()
		  return res
		}
		return false
	  }
	  
	const ctx = {
	  fetchURL
	}

	provide(&#39;GISWSContext&#39;,ctx)
	return fetchURL
	}
	
**img**:

[![enter image description here][2]][2]


  [1]: https://logaretm.com/blog/making-the-most-out-of-vuejs-injections/
  [2]: https://i.stack.imgur.com/80A5y.png

</details>


# 答案1
**得分**: 3

你的文件扩展名是错误的。它应该是 `.js`。
其次,你正在编写一个可组合性的内容,而不是一个组件。所以目录应该命名为 `composables` 而不是 `components`。

<details>
<summary>英文:</summary>

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`.

</details>



# 答案2
**得分**: 2

在Vue.js中,组件由三个部分组成:

&lt;template&gt;&lt;/template&gt;

&lt;style&gt;&lt;/style&gt;

&lt;script&gt;&lt;/script&gt;

所有的代码都必须放在这些部分中。

您的文件格式应为.vue,并且您的代码应该放在这些部分中。如果代码位于提到的部分之外,请将代码放在&lt;script&gt;标签内,或者更改文件扩展名为.js以解决此问题。

[更多信息][1]

[1]: https://vuejs.org/guide/scaling-up/sfc.html

在src/utils/GISWSCaller.js中:

```javascript
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')

如果您需要更多帮助,请告诉我。

英文:

in vue js, components are consists of three parts:

&lt;template&gt;&lt;/template&gt;

&lt;style&gt;&lt;/style&gt;

&lt;script&gt;&lt;/script&gt;

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(&#39;params:&#39;, params);

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

    const ctx = {
        fetchURL
    }

    return ctx
}

src/main.js

import {createApp} from &#39;vue&#39;
import {useGISWSCaller} from &#39;GISWSCaller&#39;

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

huangapple
  • 本文由 发表于 2023年6月12日 15:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定