|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/component-api-style |
| 5 | +description: enforce component API style |
| 6 | +--- |
| 7 | +# vue/component-api-style |
| 8 | + |
| 9 | +> enforce component API style |
| 10 | +
|
| 11 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> |
| 12 | + |
| 13 | +## :book: Rule Details |
| 14 | + |
| 15 | +This rule aims to make the API style you use to define Vue components consistent in your project. |
| 16 | + |
| 17 | +For example, if you want to allow only `<script setup>` and Composition API. |
| 18 | +(This is the default for this rule.) |
| 19 | + |
| 20 | +<eslint-code-block :rules="{'vue/component-api-style': ['error']}"> |
| 21 | + |
| 22 | +```vue |
| 23 | +<!-- ✓ GOOD --> |
| 24 | +<script setup> |
| 25 | +import { ref } from 'vue' |
| 26 | +const msg = ref('Hello World!') |
| 27 | +</script> |
| 28 | +``` |
| 29 | + |
| 30 | +</eslint-code-block> |
| 31 | + |
| 32 | +<eslint-code-block :rules="{'vue/component-api-style': ['error']}"> |
| 33 | + |
| 34 | +```vue |
| 35 | +<script> |
| 36 | +import { ref } from 'vue' |
| 37 | +export default { |
| 38 | + /* ✓ GOOD */ |
| 39 | + setup() { |
| 40 | + const msg = ref('Hello World!') |
| 41 | + // ... |
| 42 | + return { |
| 43 | + msg, |
| 44 | + // ... |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +</script> |
| 49 | +``` |
| 50 | + |
| 51 | +</eslint-code-block> |
| 52 | + |
| 53 | +<eslint-code-block :rules="{'vue/component-api-style': ['error']}"> |
| 54 | + |
| 55 | +```vue |
| 56 | +<script> |
| 57 | +export default { |
| 58 | + /* ✗ BAD */ |
| 59 | + data () { |
| 60 | + return { |
| 61 | + msg: 'Hello World!', |
| 62 | + // ... |
| 63 | + } |
| 64 | + }, |
| 65 | + // ... |
| 66 | +} |
| 67 | +</script> |
| 68 | +``` |
| 69 | + |
| 70 | +</eslint-code-block> |
| 71 | + |
| 72 | +## :wrench: Options |
| 73 | + |
| 74 | +```json |
| 75 | +{ |
| 76 | + "vue/component-api-style": ["error", |
| 77 | + ["script-setup", "composition"] // "script-setup", "composition", or "options" |
| 78 | + ] |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +- Array options ... Defines the API styles you want to allow. Default is `["script-setup", "composition"]`. You can use the following values. |
| 83 | + - `"script-setup"` ... If set, allows `<script setup>`. |
| 84 | + - `"composition"` ... If set, allows Composition API (not `<script setup>`). |
| 85 | + - `"options"` ... If set, allows Options API. |
| 86 | + |
| 87 | +### `["options"]` |
| 88 | + |
| 89 | +<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}"> |
| 90 | + |
| 91 | +```vue |
| 92 | +<script> |
| 93 | +export default { |
| 94 | + /* ✓ GOOD */ |
| 95 | + data () { |
| 96 | + return { |
| 97 | + msg: 'Hello World!', |
| 98 | + // ... |
| 99 | + } |
| 100 | + }, |
| 101 | + // ... |
| 102 | +} |
| 103 | +</script> |
| 104 | +``` |
| 105 | + |
| 106 | +</eslint-code-block> |
| 107 | + |
| 108 | +<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}"> |
| 109 | + |
| 110 | +```vue |
| 111 | +<!-- ✗ BAD --> |
| 112 | +<script setup> |
| 113 | +import { ref } from 'vue' |
| 114 | +const msg = ref('Hello World!') |
| 115 | +</script> |
| 116 | +``` |
| 117 | + |
| 118 | +</eslint-code-block> |
| 119 | + |
| 120 | +<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}"> |
| 121 | + |
| 122 | +```vue |
| 123 | +<script> |
| 124 | +import { ref } from 'vue' |
| 125 | +export default { |
| 126 | + /* ✗ BAD */ |
| 127 | + setup() { |
| 128 | + const msg = ref('Hello World!') |
| 129 | + // ... |
| 130 | + return { |
| 131 | + msg, |
| 132 | + // ... |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +</script> |
| 137 | +``` |
| 138 | + |
| 139 | +</eslint-code-block> |
| 140 | + |
| 141 | +## :mag: Implementation |
| 142 | + |
| 143 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/component-api-style.js) |
| 144 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/component-api-style.js) |
0 commit comments