`Vue3 - Vite` 项目别名 src 到 @ 不起作用

Ole*_*nko 17 javascript vue.js

我已经通过vue3 - vite导入组件安装了该项目,例如

import Component from '../../../../components/Component.vue'

我只想给 src 文件夹取别名然后做

import Component from '@/components/Component.vue'

这是我的vite.config.js

import vue from '@vitejs/plugin-vue'

/**
 * https://vitejs.dev/config/
 * @type {import('vite').UserConfig}
 */
export default {
    plugins: [
        vue({
            template: {
                compilerOptions: {
                    // ...
                }
            }
        })
    ]
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我还应该做什么?

Mis*_*ses 37

Vue 3 Vite 打字稿

vite.config.ts

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

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  }
})
Run Code Online (Sandbox Code Playgroud)

删除警告并添加下载提示@/

tsconfig.json

{
  "compilerOptions": {
    ...
    "paths": {
      "@/*": ["./src/*", "./dist/*"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 好的。感谢您。我总是收到 TS 的警告,因为我将路径设置为 `"@/*": ["./src"]`。哈哈 (4认同)

小智 22

在 vite.config.js 文件中写入以下代码块

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

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '/src'),
    },
  }
})
Run Code Online (Sandbox Code Playgroud)

  • 如果使用 vite v2.5.0 或更高版本 `'@': path.resolve(__dirname, './src'),` 。在`/src`之前添加`.` (14认同)

小智 8

我尝试了上述大部分解决方案,但不幸的是没有奏效。下面做了。

在我的 jsconfig.json 中

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的 vite.config.js 中

import { fileURLToPath } from "url";
import path from "path";

import { defineConfig } from "vite";

export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(path.dirname(fileURLToPath(import.meta.url)), "src"),
    },
  },
});
Run Code Online (Sandbox Code Playgroud)


mat*_*tch 7

webpack,默认情况下Vite没有@别名src。在以下问题上对此进行了讨论:https : //github.com/vitejs/vite/issues/279

最终你需要创建一个别名:

// vite.config.js 
module.exports = {
  alias: {
    '/@': path.resolve(__dirname, './src')
  }
}

// Your config 
export default {
  alias: {
    '/@': path.resolve(__dirname, './src')
  },
  plugins: [ ... ]
 }
Run Code Online (Sandbox Code Playgroud)

然后你需要/@在你的文件中使用,例如:

import foo from '/@foo'
Run Code Online (Sandbox Code Playgroud)


rol*_*oli 6

这对我有用:

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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@/': `${path.resolve(__dirname, 'src')}/`
    }
  }
})

Run Code Online (Sandbox Code Playgroud)

然后在组件中:

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)