英文:
Vue.js issue with typescript - alias has no error in code but when running shows an error that can't resolve alias
问题
I have created the most basic app to test this issue, not sure if I haven't configured something right, but the issue is that importing a typescript file into my .vue component will create no error and the compiler can find it, however when typing npm run serve
an error will appear saying `Can't resolve '@logic/enemy-repository' in '~\enemytest\src\views'. Have recreated the app many times with different configurations but still the same issue. Any ideas?
This is all being done in VS Code, Vue.js 3 and on windows.
Just to note:
- I'm referencing like so
import EnemyRepository from '@logic/enemy-repository'
- Doing relative referencing of the file causes no issue such as
import EnemyRepository from '../logic/enemy-repository'
- Doing the import from the route absolutely also works
import EnemyRepository from '@/logic/enemy-repository'
- Because of this the issue is most likely to do with the tsconfig.json file. My paths and baseurl are set up like so...
"baseUrl": ".",
"paths": {
"@//": [
"src/"
],
"@logic/": ["src/logic/"],
},
Steps to recreate:
- vue create enemytest
- Manually select features
- Babel, Typescript, Router, vuex, linter/formatter selected.
- 3.x
- Use class-style component syntax
- Use babel alongside typescript
- Use history mode
- ESLint with error prevention only
- Lint on save
- In dedicated config file
- Open in vs code.
- Create a new file in folder structure /src/loci/enemy-repository.ts and export method.
- Import repository into HomeView.vue.
- In tsconfig.json add a path new item to paths "@logic/": ["src/logic/"],
- Type in console npm run serve
- Error shows in terminal 'Module not found: Error: Can't resolve '@logic/enemy-repository' in '~\enemytest\src\views''
Have attached a repo here GitHub Repo
英文:
I have created the most basic app to test this issue, not sure if I haven't configured something right, but the issue is that importing a typescript file into my .vue component will create no error and the compiler can find it, however when typing npm run serve
an error will appear saying Can't resolve '@logic/enemy-repository' in '~\enemytest\src\views
. Have recreated the app many times with diffrent configurations but still the same issue. Any ideas?
This is all being done in VS Code, Vue.js 3 and on windows.
Just to note:
-
I'm referencing like so
import EnemyRepository from '@logic/enemy-repository'
-
Doing relative referencing of the file causes no issue such as
import EnemyRepository from '../logic/enemy-repository'
-
Doing the import from the route absolutely also works
import EnemyRepository from '@/logic/enemy-repository'
-
Because of this the issue is most likely to do with the tsconfig.json file. My paths and baseurl are set up like so...
"baseUrl": ".", "paths": { "@/*": [ "src/*" ], "@logic/*": ["src/logic/*"], },
Steps to recreate:
- vue create enemytest
- Manually select features
- Babel, Typescript, Router, vuex, linter/formatter selected.
- 3.x
- USe class-style component syntax
- Use babel alongside typescript
- Use history mode
- ESLint with error prevention only
- Lint on save
- In dedicated config file
- Open in vs code.
- Create a new file in folder structure /src/loci/enemy-repository.ts and export method.
- Import repository into HomeView.vue.
- In tsconfig.json add a path new item to paths "@logic/": ["src/logic/"],
- Type in console npm run serve
- Error shows in terminal 'Module not found: Error: Can't resolve '@logic/enemy-repository' in '~\enemytest\src\views''
Have attached a repo here GitHub Repo
答案1
得分: 3
- 对于
@vue/cli
项目:
// vue.config.js
const path = require('path');
module.exports = {
configureWebpack: {
resolve: {
alias: {
"@logic": path.resolve(__dirname, 'src/logic/'),
"@": path.resolve(__dirname, 'src/')
}
}
}
}
- 对于
vite
项目:(resolve.alias)
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
export default defineConfig({
resolve:{
alias:{
'@logic': path.resolve(__dirname, './src/logic'),
'@' : path.resolve(__dirname, './src')
},
},
plugins: [vue()]
})
英文:
- for
@vue/cli
projects:
// vue.config.js
const path = require('path');
module.exports = {
configureWebpack: {
resolve: {
alias: {
"@logic": path.resolve(__dirname, 'src/logic/'),
"@": path.resolve(__dirname, 'src/')
}
}
}
}
- for
vite
projects: (resolve.alias)
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
export default defineConfig({
resolve:{
alias:{
'@logic': path.resolve(__dirname, './src/logic')
'@' : path.resolve(__dirname, './src')
},
},
plugins: [vue()]
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论