unplugin / unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup
MIT License
3.19k stars 197 forks source link

Auto import custom directives doesn't works, is it supported? #369

Open yyq-bit opened 1 year ago

yyq-bit commented 1 year ago

Describe the bug

I defined an custom directives - vEllipsis

composables/ellipsis

import type { ObjectDirective } from 'vue';

export const vEllipsis: ObjectDirective = {
  mounted(el: HTMLElement) {
    el.style.whiteSpace = 'nowrap';
    el.style.overflow = 'hidden';
    el.style.textOverflow = 'ellipsis';
    el.setAttribute('title', el.innerHTML);
  },
};

It works when import vEllipsis,auto import succeed,but it doesn't works.

// app.vue
<script setup lang="ts">
import { vEllipsis } from './composables/ellipsis'; // it works
</script>

<template>
  <div v-ellipsis class="wrap">
    我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话
  </div>
</template>
// vite.config.ts
...
AutoImport({
  dts: 'src/auto-imports.d.ts',
  dirs: ['src/composables/**'],
}),
...

Reproduction

https://stackblitz.com/edit/vitejs-vite-p9rpye?file=src%2FApp.vue,src%2Fauto-imports.d.ts,package.json&terminal=dev

System Info

vue: ^3.2.47
vite: ^4.3.0
unplugin-auto-import: ^0.15.2

Used Package Manager

npm

Validations

ListeningRift commented 1 year ago

The auto-import directive is supported, but you are not using it correctly. According to the example provided in the playground, the correct usage should be like this:

<script setup lang="ts">
// import { ellipsis } from "./composables/ellipsis";
</script>

<script lang="ts">
export default {
  directives: {
    ellipsis: vEllipsis,
  },
};
</script>

<template>
  <div v-ellipsis class="wrap">
    我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话我是很长的一段话
  </div>
</template>

<style scoped>
.wrap {
  width: 200px;
}
</style>

You need

lishaobos commented 1 year ago

unplugin-vue-components also can resolve your problem

sakarnDev commented 10 months ago

unplugin-vue-components also can resolve your problem

how??