-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Component static method #480
Comments
This would be pretty simple to implement in a naive way - we could just keep that assign( component, template.staticMethods( component ) ); but I'm wondering whether there's some more static analysis we ought to be doing if we implemented this. |
Can definitely see the value in being able to declare static methods (and properties). Can also see why it makes sense to implement as a function, since Given that, what if it's a more general purpose setup hook, like this? export default {
data() {
value: ''
},
// The component constructor need to be provided to those methods.
setup (Component) {
console.log( 'adding some static methods and properties' );
Component.from = function ( textarea ) {
// ...
};
Component.SOME_CONSTANT = 42;
}
}; In other words, Svelte makes no assumptions about whether the setup work involves declaring static methods, or overriding built-in methods for some esoteric debugging purposes or whatever — it's just a convenient place to do that work.
It does move us away from a world where the compiler can know almost everything about the component and how it can be used, into one where the behaviour becomes a black box. But as you point out, someone could do that anyway since it's just a regular JS constructor, they'd just be doing it in an even less predictable (to the compiler) way. |
@Rich-Harris what about static methods and properties in v3? |
You can do this in v3 by using the <script context="module">
export const toUpper = str => str.toUpperCase();
</script> This function can then be imported and used from anywhere: import { toUpper } from './Component.svelte';
const myStr = toUpper('hi'); See the |
@pngwn Thanks! But your reply about named export, not about static methods or properties. It is different things. For example static methods/properties are more convenient in some cases, when you need more coherence between a class and meta-data when this bunch traveling through a program. For example: <script>
export let Item;
</script>
{#if Item.TYPE === 1}
<svelte:component this={Item} foo bar={32} />
{:else}
<div class="wrapper">
<svelte:component this={Item} />
</div>
{/if} Yes, I can pass a object with TYPE and Item, but it is hack. Isn't it? <script>
export let obj;
</script>
{#if obj.TYPE === 1}
<svelte:component this={obj.Item} foo bar={32} />
{:else}
<div class="wrapper">
<svelte:component this={obj.Item} />
</div>
{/if} |
Come to one of the support channels (Stack Overflow or Discord Chat) and we can help you with this. Github isn't the place for support questions. |
Static methods could be used to define alternative constructors. For example, a rich text editor component could define a
from
static method initialising the component data from an existing textarea:It's easy enough to define those methods in an other module, but having those kind of methods defined with the component would be tidier.
The text was updated successfully, but these errors were encountered: