eslint-plugin-vue icon indicating copy to clipboard operation
eslint-plugin-vue copied to clipboard

`prefer-builtin-properties-and-methods`

Open fisker opened this issue 4 months ago • 0 comments

Please describe what the rule should do:

Prefer use $emit, $slots, etc in <template> instead of variables defined in <script>.

What category should the rule belong to?

[x] Enforces code style (layout) [ ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

<!-- Bad -->
<script setup>
const emit = defineEmits('foo')
</script>
<template>
	<button @click="emit('foo')">Foo</button>
</template>
<!-- Good -->
<script setup>
const emit = defineEmits('foo')
</script>
<template>
	<button @click="$emit('foo')">Foo</button>
</template>
<!-- Bad -->
<script setup>
import { useSlots, useAttrs } from 'vue'

const slots = useSlots()
</script>
<template>
	<div>
		<div v-if="slots.foo"><slot name="foo"></slot></div>
	</div>
</template>
<!-- Good -->
<script setup>
import { useSlots, useAttrs } from 'vue'

const slots = useSlots()
</script>
<template>
	<div>
		<div v-if="$slots.foo"><slot name="foo"></slot></div>
	</div>
</template>

Additional context

It can also apply to other properties and methods

fisker avatar Sep 29 '24 13:09 fisker