Skip to content

Instantly share code, notes, and snippets.

@viT-1
Last active August 4, 2023 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viT-1/238d1ad4da4d46b394851174105decea to your computer and use it in GitHub Desktop.
Save viT-1/238d1ad4da4d46b394851174105decea to your computer and use it in GitHub Desktop.
#gist-bookmark #vue

Vue3 (Composition API) не поддерживает (script setup src) традиционную схему разделения кода на esm-модули, так как принуждает прописывать код в *.vue-файлах (SFC). Соответственно тонко рулить import'ами похоже не выйдет.

Более того, если ранее с использованием vue-class-component приучали к "чистому коду" (всё что требуется импортируем явно), то теперь с такими конструкциями как defineProps вновь идёт откат к использованию окружения.

Too much magic!

Workaround: SFC.vue (non setup, js transpiled by webpack etc. compilers):

<template>...</template>
<script>
import { defineComponent } from 'vue';
import { opts } from './SomeComponent';

// export default only: https://github.com/vuejs/vue-loader/issues/1234#issuecomment-380222855
export default defineComponent(opts);
</script>

SomeComponent.js:

import { reactive } from 'vue';

const opts = {
// use props as optionsAPI, not defineProps
    props: {
        params: Object,
    },
    setup (props) {
        const items = reactive(props.params.items);

        // "computed"
        return {
            items,
        };
    }
}

export { opts };
@viT-1
Copy link
Author

viT-1 commented Jul 31, 2023

script setup is DSL and not spec-compliant JavaScript. It's not transparent and not thoroughly documented, a considerable amount of the problems that are related to this syntax come from that the developers aren't fully aware of what code is doing, this can be confirmed only by debugging the compiled JavaScript code.

from stackoverflow

@viT-1
Copy link
Author

viT-1 commented Jul 31, 2023

Заметьте, что импортировать defineProps и defineEmits не нужно. Это специальные макросы, которые использует Vue. Они обрабатываются во время компиляции в «обычный» синтаксис Options API. Скорее всего, мы будем видеть всё больше и больше подобных макросов в будущих релизах.

отсюда

@viT-1
Copy link
Author

viT-1 commented Jul 31, 2023

Кроме того, продвигаются анти-паттерны в виде default export:

The component itself must be the default export. That's a requirement.

отсюда

@viT-1
Copy link
Author

viT-1 commented Jul 31, 2023

[Vue warn]: defineProps() is a compiler-hint helper that is only usable inside <script setup> of a single file component.

@viT-1
Copy link
Author

viT-1 commented Jul 31, 2023

@viT-1
Copy link
Author

viT-1 commented Aug 4, 2023

Промежуточная конфигурация между options API и setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment