Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue 3 The template root requires exactly one element. eslint-plugin-vue

After scaffolding a Vue 3 project I noticed an error in my App.vue.

A functional component that renders the matched component for the given path. Components rendered in can also contain its own , which will render components for nested paths.

API Reference

[vue/no-multiple-template-root]
The template root requires exactly one element.eslint-plugin-vue

I tried putting

"vue/no-multiple-template-root": 0

in my .eslintrc.js

But the error remains. How can I get rid of the error? Since in Vue 3 you don't have to have exactly one element in a template.

module.exports = {
    root: true,
    env: {
        node: true
    },
    extends: [
        "plugin:vue/vue3-essential",
        "eslint:recommended",
        "@vue/typescript/recommended",
        "@vue/prettier",
        "@vue/prettier/@typescript-eslint"
    ],
    parserOptions: {
        ecmaVersion: 2020
    },
    rules: {
        "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
        "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
        "vue/no-multiple-template-root": 0
    }
};
like image 346
anonymous-dev Avatar asked Nov 17 '20 00:11

anonymous-dev


People also ask

How many elements does the Vue template root require?

Vue 3 The template root requires exactly one element.eslint-plugin-vue 26 December, 202026 December, 2020 print [vue/valid-template-root] The template root requires exactly one element.eslint-plugin-vue

Does the template root require exactly one element in the template?

Components rendered in can also contain its own , which will render components for nested paths. API Reference [vue/no-multiple-template-root] The template root requires exactly one element.eslint-plugin-vue But the error remains. How can I get rid of the error? Since in Vue 3 you don't have to have exactly one element in a template.

Why is my vuejs template validation not working?

If you don't have any package.json in your project's root directory, vetur can't understand which version of vue.js you're using and assumes it is less then 2.5. If the version is wrong, you will get wrong template validation.

How do I ask a question about Vue?

Be descriptive about your question or problem and be sure to include any errors. Single line topics will rarely receive answers and do not just post a screenshot. Be specific about which version of Vue and its related plugins you’re using. Link to any 3rd party libs/plugins that relate to your question.


6 Answers

I ended up turning off Vetur linting. Vetur thinks that it is a Vue 2 project becuase it is in a VS Code workspace.

https://github.com/vuejs/vetur/issues/2299#issuecomment-696015374

You can solve this by doing

F1>Preferences:Open Settings (JSON)

paste

"vetur.validation.template": false,
"vetur.validation.script": false,
"vetur.validation.style": false,
like image 166
anonymous-dev Avatar answered Oct 16 '22 21:10

anonymous-dev


The error occurred with me, because I had not opened the project root in VS Code, but the parent folder, and Vetur thus looked in this for the package.json.

like image 37
skalta Avatar answered Oct 16 '22 20:10

skalta


Here worked with this:

in .eslintrc.js

instead of:

rules: { "vue/no-multiple-template-root": 0 }

try:

rules: {"vue/no-multiple-template-root": "off" }
like image 17
Everton Nogueira Avatar answered Oct 16 '22 22:10

Everton Nogueira


If you are working on a monorepo and thus the package.json is not at the workspace root, then Vetur by default won't able to find package.json, thus it couldn't figure out the vue version.

In that case, we need to tell Vetur the package.json location

https://vuejs.github.io/vetur/guide/setup.html#advanced

enter image description here

like image 16
Tianzhou Avatar answered Oct 16 '22 20:10

Tianzhou


With vue create project on Vue3, add this to your package.json file:

"eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {
      "vue/no-multiple-template-root": "off"
    }
  }

Adding the "rules" eliminates the error as described in the question. No need to change the default vetur settings.

like image 7
Dimitri Mostrey Avatar answered Oct 16 '22 20:10

Dimitri Mostrey


Disable Vetur and use Volar instead. It's now the official recommendation for Vue 3 projects.

From the official migration guide:

It is recommended to use VSCode with our official extension Volar (opens new window), which provides comprehensive IDE support for Vue 3.

like image 8
Mahmoud Avatar answered Oct 16 '22 22:10

Mahmoud