[英]How to enforce script, template and style tags order in vue with linter
I want to add a linter rule that checks that the tags are in this order: <script>
, <template>
and <style>
.我想添加一个检查标签是否按以下顺序的 linter 规则: <script>
、 <template>
和<style>
。 Vue's default is <template>
first, but I want the <script>
because I consider it more important. Vue 的默认是<template>
首先,但我想要<script>
因为我认为它更重要。
It'd be awesome that this could be auto-fixed.可以自动修复这真是太棒了。
I couldn't find this rule for eslint, and if there isn't any I may consider creating it.我找不到 eslint 的这条规则,如果没有,我可以考虑创建它。
eslint-plugin-vue
supports the component-tags-order
rule for this. eslint-plugin-vue
支持component-tags-order
规则。 It's already included in "plugin:vue/vue3-recommended"
and "plugin:vue/recommended"
rule sets (if using a Vue CLI scaffolded project).它已经包含在"plugin:vue/vue3-recommended"
和"plugin:vue/recommended"
规则集中(如果使用 Vue CLI 脚手架项目)。
The config to enforce <script>
, <template>
, and then <style>
:强制执行<script>
、 <template>
和<style>
的配置:
// .eslintrc.js
module.exports = {
rules: {
'vue/component-tags-order': ['error', {
order: [ 'script', 'template', 'style' ]
}],
}
}
Note the rule does not implement auto-fixing, so you'll only get linter errors.请注意,该规则不实现自动修复,因此您只会得到 linter 错误。
However, there's a command-line utility ( v-change-tags-order
) that you could run to rearrange the tags:但是,您可以运行一个命令行实用程序 ( v-change-tags-order
) 来重新排列标签:
npx v-change-tags-order
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.