Skip to content
| Marketplace
Sign in
Visual Studio Code>Formatters>format-vue-style-codeNew to Visual Studio Code? Get it now.
format-vue-style-code

format-vue-style-code

leemathew1998

|
808 installs
| (0) | Free
format、sort you vue2 code
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Format Vue Style Code

Features

1️⃣ 重排 import 文件顺序 ✅

重排前
let timer;
import App from "./App.vue";
import { func1 } from "@/utils/func.js";
import { func2 } from "@/utils/func.js";
import Vue from "vue";
重排后
import Vue from "vue";
import { func1, func2 } from "@/utils/func.js";
import App from "./App.vue";
let timer;

引入文件将会按照以下规则进行排序,并且自动去除重复引入的文件。

  1. 依赖文件
  2. 带有@或者~的引入文件
  3. 相对路径引入的文件或者定义的变量

2️⃣ 对 Vue2 中的 option API 写法进行格式化 ✅

重排前
export default {
  methods: {    3️⃣
    func1() {},
  },
  data() {      2️⃣
    return {};
  },
  name: "App",  1️⃣
};
重排后
export default {
  name: "App",  1️⃣
  data() {      2️⃣
    return {};
  },
  methods: {    3️⃣
    func1() {},
  },
};

将按照 name、components...等顺序 进行排列。

  • 如何修改默认顺序? 快捷键:ctrl + shift + p 或者 command + shift + p,输入format-vue-style-code: change option order,输入你想要的排序即可。

3️⃣ 对 data 等 options 中的内容进行排序 ✅

template part
<template>
  <div>
    <div>{{ a }}</div>
    <div>{{ b }}</div>
  </div>
</template>
script part
export default {
  data() {
    return {
        b: 2,
        a: 1,
    };
  },
  created() {
    console.log(this.a)
  },
};
  • 在此例子中,data 中的 a: 1 将会被放到 b: 2 的前面;因为不管在template中还是生命周期函数中,a 变量都是优先使用的。如果有冲突,那会以生命周期函数 > template进行排序。
  • 此外,我们对日常使用的option(components、computed、data、filters、methods、props、watch)都进行了类似的排序:
    • 第一个用到的component将会排在components的第一位置
    • 第一个用到的method将会排在methods的第一位置
    • ......

How to use

  • 右键菜单中的Format You Code选项
  • 快捷键:ctrl + shift + p 或者 command + shift + p,输入Format You Code

Undone Features

  • [ ] 对 Vue3 中的 composition API 进行格式化
  • [ ] 代码重构

Download

在 vscode 扩展中搜索 format-vue-style-code 即可找到该插件。

Issues

使用中遇到问题可以在这里提问。 https://github.com/leemathew1998/format-you-code/issues

Sources

插件源码,沟通交流在这里。 https://github.com/leemathew1998/format-you-code

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft