Elentra JS Documentation
2.0
2.0
  • Introduction
  • Why Elentra JS 2.0?
    • Dedicated Repository
    • TypeScript
    • Class-Based Components
    • Vue Standard Routing
    • Organizational Theming
    • Yarn Package Manager
  • Developers
    • Getting Started
    • Setup Guide
      • Install Requirements
      • Clone the Repository
      • Environment Variables
      • Install Dependencies
      • Serve Dev Mode
  • Design System
    • Overview
    • Component Library
      • Directory Structure
      • Components
    • Style Library
      • Directory Structure
      • How it Works
        • Variables
        • Mixins
        • Utility Classes
        • Elements
        • Legacy Styles
        • Shim
    • Taxonomy
      • UI Taxonomy
      • UX Taxonomy
      • Variable & Utility Class Naming Convention
Powered by GitBook
On this page
  1. Why Elentra JS 2.0?

Class-Based Components

PreviousTypeScriptNextVue Standard Routing

Last updated 5 years ago

Alongside the switch to , the components that make up the have each been rewritten as class-based components. Interacting with these components in Vue.js template code is the same as before, and passing props and handling events will work the same way, however the script definitions for components are now written as classes with decorators.

This is made possible due to a library called , which itself makes use of another popular library called . This switch to a class structure for our components comes with all the obvious benefits that come with object-oriented design, and will result in enhanced readability and better organization of our Vue.js component code.

A brief example of a class-based component using decorators may be found below.

ExampleComponent.vue
<template>
    <EjsButton type="info" @click="toggleMessageVisibility()">
        {{ buttonLabel }}
    </EjsButton>
    <div v-if="messageVisible" class="message">
        {{ message }}
    </div>
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'

import {
    EjsButton,
} from '@/components';

@Component({
    components: {
        EjsButton,
    }
})
export default class ExampleComponent extends Vue {

    /* --------------- Data --------------- */
    private messageVisible!: boolean = false;
    private target!: string = 'World';

    /* --------------- Props --------------- */
    @Prop()
    private greeting: string = 'Hello';

    /* --------------- Computed --------------- */
    get buttonLabel() {
        return this.messageVisible
        ? 'Hide Message' 
        : 'Show Message';
    }
        
    get message() {
        return `${this.greeting}, ${this.target}`;
    }
    
    /* --------------- Methods --------------- */    
    private toggleMessageVisibility(visibleState?: boolean) {
        visibleState === undefined
        ? this.messageVisible = !this.messageVisible        // Toggle
        : this.messageVisible = visibleState;               // Set To Arg
    }
}
</script>
TypeScript
Component Library
vue-property-decorator
vue-class-component