svelte
svelte copied to clipboard
Define static properties/methods against a component
Describe the problem
Using context='module' we can declare module-level properties for a component.
However, you then have to assign new variables to address the exported properties. If you import multiple components using a similar pattern then you also have to rename them like this:
import FooModal, { events as fooEvents } from './FooModal';
import BarModal, { events as barEvents } from './BarModal';
// fooEvents.on(...)
// barEvents.on(...)
// <FooModal />
// <BarModal />
Describe the proposed solution
It would be much nicer if we could declare static properties/methods against a component so that we can use them like this:
import FooModal from './FooModal';
import BarModal from './BarModal';
// FooModal.on(...)
// BarModal.on(...)
// <FooModal />
// <BarModal />
Alternatives considered
- Currently using
import FooModal, { events as fooEvents }, but it's clumsy and verbose. - Could set the static properties in an import file outside the component but this is very hacky and even more clumsy.
- Not aware of any other way to set static properties on a component.
Importance
would make my life easier
did you try:
<script>
import * as FooModal from "./FooModal.svelte"
FooModal.someStaticMethod()
</script>
<FooModal.default {...props} />
That's an interesting idea but these are generic components that are used frequently, I shouldn't have to pollute everything with .default all over the place.