Home > Web Front-end > Vue.js > How to solve the error when vue3 uses alias

How to solve the error when vue3 uses alias

王林
Release: 2023-05-11 09:43:05
forward
1078 people have browsed it

Solve the problem of error reporting when vue3 uses aliases:

On vue-cli3 and above, when creating a vue3 project and using typescript, the alias will be automatically configured.

See tsconfig.json under the project root path

"baseUrl": ".",
"paths": {
      "@/*": [
        "src/*"
      ]
    }
"include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ]
Copy after login

The first is that your path is correct. If the vetur plug-in is used, It keeps reporting that the alias path cannot be found

So you can modify the configuration directly in the configuration of the vetur plug-in

Open the settings and find the vetur plug-in

How to solve the error when vue3 uses alias

Find setting.json Configure

How to solve the error when vue3 uses alias

in setting.json Add "vetur.validation.script": false

How to solve the error when vue3 uses alias

to solve the vetur error reporting problem

There is no need to add vue.config.js file in this project

Attachment: Alias ​​settings in vue

For 3.0 Vue, create new vue.config.js.

const path = require('path')
function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    configureWebpack: {
        resolve: {
            alias: {
                '#': resolve('src'),
            }
        }
    },
}
Copy after login

In our routing file, it can be written as

import Login from '#/views/Login.vue'
Copy after login

For another example, we set the alias of the src/views directory to views

const path = require('path')
 
function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    configureWebpack: {
        resolve: {
            alias: {
                '#': resolve('src'),
                'views':resolve('src/views'),
            }
        }
    },
 
}
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from 'views/Login.vue'
Copy after login

Remarks: Self-built vue .config.js and default configuration are merged into the final configuration.

The above is the detailed content of How to solve the error when vue3 uses alias. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template