Vue3组合式Api script setup模式中顶层使用await报Top-level ‘await‘ expressions are only allowed when the ‘module‘

今天练习Vue3的Suspense组件的时候碰到在Vue3组合式Api script setup模式中顶层使用await时报错Eslint错误(能正常编译),错误提示是:

Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.Vetur(1378)

如截图:

解决方案1:检查项目中是否是使用了Vetur

检查项目中是否是使用了Vetur,检查项目中是否是使用了Vetur,检查项目中是否是使用了Vetur。

重要的事情说3遍,Vue3不能搭配Vetur,会有很多奇葩的格式校验问题。

如果安装了,并且启用状态,将其Disable掉就好,可以仅禁用当前工程。因为多数时候我们会同时维护vue2项目和vue3项目。

 

Vue3搭配的是Volar。

解决方案2:修改项目中的tsconfig.json

一般情况下,使用方案1就都解决了,如果方案1还有问题,可以尝试一下方案2。

修改项目中的tsconfig.json

原配置文件:

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.node.json"
    },
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.vitest.json"
    }
  ]
}

修改为:


{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.node.json"
    },
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.vitest.json"
    }
  ],
  // https://www.typescriptlang.org/tsconfig#compilerOptions
  "compilerOptions": {
    "esModuleInterop": true,
    "lib": ["es2020"],
    "module": "es2022",
    "preserveConstEnums": true,
    "moduleResolution": "node",
    "strict": true,
    "sourceMap": true,
    "target": "es2022",
    "types": ["node"],
    "outDir": "dist"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

参考文档:

javascript - Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext' - Stack Overflow

至此,问题搞定。

这里贴上Suspense相关的测试代码,文档参考官方文档:

Suspense | Vue.js

Async1.vue,单文件组件,将会被AsyncWrapper.vue以异步组件的方式加载:

<template>
  <div>
    <h2>Async1</h2>
  </div>

</template>
<script lang="ts" setup>
  console.log('[Async1.vue] setup :: enter')
</script>

Async2.vue,单文件组件,将会被AsyncWrapper.vue以异步组件的方式加载:

<template>
  <div>
    <h2>Async2</h2>
  </div>

</template>
<script lang="ts" setup>
  console.log('[Async2.vue] setup :: enter')
</script>

Async3.vue,单文件组件,script setup的顶层代码中使用await

<template>
  <div>
    <h2>Async3, use await</h2>
    <h2> {{ refData }} </h2>
  </div>

</template>
<script lang="ts" setup>
  import { ref } from 'vue'
  console.log('[Async3.vue] setup :: enter')
  const p = new Promise((resolve,reject)=> {
    setTimeout(() => {
      resolve('Async 3 ok~~~')
    }, 5000)
  })
  const data =await p
  console.log('[Async3.vue] setup :: async end')
  const refData = ref(data)
</script>

AsyncWrapper,以异步方式加载Async1.vue、Async2.vue,导入底层具备await的组件。AsyncWrapper成为一个异步组件容器,有3个异步依赖项。

<template>
  <div>
    <h2>AsyncWrapper</h2>
    <Async1></Async1>
    <Async2></Async2>
    <Async3></Async3>
  </div>

</template>
<script lang="ts" setup>
import {ref, defineAsyncComponent} from 'vue'
// script setup 单文件组件,顶层使用了await
import Async3 from './Async3.vue'
console.log('[AsyncWrapper.vue] setup :: enter')

// 异步组件,异步的加载单文件组件
const Async1 = defineAsyncComponent(()=> {
  return new Promise((resolve, reject)=> {
    setTimeout(()=> {
      import('./Async1.vue').then((res)=> {
        resolve(res)
      })
    }, 2000)
  })
})
// 异步组件,异步的加载单文件组件
const Async2 = defineAsyncComponent(()=> {
  return new Promise((resolve, reject)=> {
    setTimeout(()=> {
      import('./Async2.vue').then((res)=> {
        resolve(res)
      })
    }, 4000)
  })
})

</script>

AsyncTester.vue,使用Suspense统一管理几个异步加载的组件内容项

<template>
  <h2>测试异步组件相关逻辑</h2>
  <Suspense>
    <AsyncWrapper />
    <template #fallback>
      加载中。。。
    </template>
  </Suspense>
</template>

<script lang="ts" setup>
import AsyncWrapper from '../components/AsyncWrapper.vue'
</script>

// 结果满足预期

1,Suspense启动的时候,在内存中渲染默认插槽内容组件,AsyncWrapper,发现里面有异步加载组件Async1.vue、Async2.vue以及顶层有await的Async3.vue,因此进入挂起状态,显示#fallback插槽,界面上看到加载中。

2,等3个异步等完成之后,三个组件内容,同时渲染到界面上。

3,这在管理多个异步加载组件内容的时候,感觉还是很有用的。

风险: 这还是一个实验性的内置组件,它不一定会最终成为稳定功能,并且在稳定之前相关 API 也可能会发生变化。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3组合式API使用vue-baidu-map-3x自定义地图心点,可以按照以下步骤: 1. 安装vue-baidu-map-3x 你可以通过以下命令使用npm安装vue-baidu-map-3x: ``` npm install vue-baidu-map-3x --save ``` 2. 在组合式API引入vue-baidu-map-3x 在需要使用百度地图的组件,你需要先引入vue-baidu-map-3x。例如,在一个名为Map.vue的组件,你可以这样引入: ``` <script> import { defineComponent } from 'vue' import BaiduMap from 'vue-baidu-map-3x' export default defineComponent({ components: { BaiduMap }, setup() { // 组合式API的代码 } }) </script> ``` 3. 在setup函数定义地图心点 在setup函数,你可以定义地图的心点和缩放级别。例如: ``` <script> import { defineComponent } from 'vue' import BaiduMap from 'vue-baidu-map-3x' export default defineComponent({ components: { BaiduMap }, setup() { const center = { lng: 116.404, lat: 39.915 } const zoom = 12 return { center, zoom } } }) </script> ``` 在这个例子,我们定义了地图的心点为北京市心,缩放级别为12。 4. 在模板使用地图 最后,在模板使用vue-baidu-map-3x组件,并且传入定义的心点和缩放级别。例如: ``` <template> <div class="map-container"> <baidu-map :center="center" :zoom="zoom"></baidu-map> </div> </template> ``` 现在,你已经在Vue3组合式API使用vue-baidu-map-3x自定义了地图的心点。你可以通过修改center的值来改变地图的心点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值