Vue3项目:ts通过import引入传入文件路径报找不到该模块

前言

在项目开发中需要通过封装的方法来减小体积,在封装一个异步方法时,传入的路径明明正确却报找不到

浏览器报错:
在这里插入图片描述
控制台报错:
Critical dependency: the request of a dependency is an expression

我的代码:

loader = () => import(path)

解决心路历程

采用引入文件报错后,我尝试使用写死的路径,发现可以正常使用,没有任何问题!!!最后查看文档发现 import只能使用字符串!! require相同。最后采用字符串拼接方法

更改后的代码:

import('@/' + path + '.vue')

PS:import使用模板字符串会报错,require不会

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
当你在 Vue项目中使用 Vuex 进行状态管理时,可以通过在 Store 中定义 TypeScript 类型,并将其引入Vue 文件中进行使用。下面是一个示例: 先,在 Vuex 的 Store 文件中定义类型: ```typescript // store.ts import { createStore } from 'vuex'; interface RootState { count: number; todos: string[]; } export default createStore<RootState>({ state: { count: 0, todos: [], }, mutations: { // mutations... }, actions: { // actions... }, getters: { // getters... }, }); ``` 在上述示例中,我们定义了 `RootState` 类型,表示整个 Vuex store 的根状态。然后,我们使用 `createStore<RootState>()` 函数创建了一个 Vuex store,并传入了 `RootState` 类型参数。 接下来,在 Vue 文件中使用该 Store: ```vue <template> <div> <p>Count: {{ count }}</p> <ul> <li v-for="todo in todos" :key="todo">{{ todo }}</li> </ul> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import { useStore } from 'vuex'; export default defineComponent({ computed: { count(): number { return this.$store.state.count; }, todos(): string[] { return this.$store.state.todos; }, }, }); </script> ``` 在上述示例中,我们使用 `useStore` 函数从 Vuex 中获取 store 实例,并通过 `$store.state` 访问其中的状态。根据在 Store 中定义的类型,我们可以在计算属性中指定相应的类型,以确保类型安全。 通过以上步骤,我们可以在 Vuex Store 中定义 TypeScript 类型,并在 Vue 组件中使用这些类型来获得类型检查和自动补全的好处。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值