我有一个与维生素EBundle 在一起的VUE应用程序.在将依赖项作为VUE单文件组件加载时,我遇到了一个问题.一切都是按照标准的脚手架VITE示例进行的.

这是我的vite.config.js美元

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      // This SHOULD resolve my imports, but it seems to have a problem
      '@': fileURLToPath(new URL('./src', import.meta.url))    
    }
  }
})

以下是我try 导入依赖项的VUE router.js文件

import welcomeChat from "@/views/welcome-chat"   //<======= this throws an error

const router = createRouter({
 history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
        path: "/welcome",
        name: "welcome",
        component: welcomeChat
    }
})

npm run dev可以,但当我在浏览器中打开我的应用程序时,我会得到以下error

Failed to load url /src/views/welcome-chat (resolved id: /Users/doogie/Coding/liquido/liquido-mobile-pwa-vue3_3/src/views/welcome-chat) in /Users/doogie/Coding/liquido/liquido-mobile-pwa-vue3_3/src/services/router.js. Does the file exist?

/Users/doogie/Coding/liquido/liquido-mobile-pwa-vue3_3/src/views/welcome-chat章确实存在使用.vue扩展名有问题吗?

推荐答案

您必须在vite.config.jsresolve部分设置extensions属性才能添加vue分机

请参见here(默认为['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'])

你的vite配置应该看起来像

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    alias: {
      // This SHOULD resolve my imports, but it seems to have a problem
      '@': fileURLToPath(new URL('./src', import.meta.url))    
    }
  }
})

Vue.js相关问答推荐

在Nuxt项目中混合Scrolltrigger与Lenis

如何在与父集合相同的读取中取回子集合(或重构数据?)

设置Vite Vue 3需要多个应用程序输出目录

Vutify-v-工具提示-不使用键盘轻击按钮显示

Vue 3 / Vuetify 3:如何为按钮设置全局默认 colored颜色

Vue 脚本标签中更漂亮的 destruct 功能

Nuxt 2.15.7 的构建错误 - 无法解析 CSS @font-face URL

如何使用 vue 或 javascript 向服务器发送密码?

Axios Intercept 在第一次调用时未从 localStorage 应用令牌

在 Vue 项目之间共享assets和组件

如何在 Vue.js 中添加动态组件/部分

事件 click点击的无效处理程序:未定义

如何在nuxt路由中添加meta?

Laravel 事件未广播

如何在两个属性上使用 Vue/Vuetify 自动完成过滤器?

try 通过 Vue.js 中的渲染传递props并观察它们

vue-cli-service build --target lib 导入为 lib 时丢失图像路径

Vue Chart.js - 条形图上的简单点/线

将登录页面包含在单页应用程序中是否不安全?

在~/components/AddPlaceModal.vue中找不到导出AddPlaceModal