Use Statement

Introduction

One of the unique and powerful features of EJS is its approach to managing dependencies. Combining the succinctness of synchronous syntax with the performance benefits of asynchronous, batched file loading allows each class to declare its dependencies using simple use statements.

const MyClass = use('MyNamespace/MyClass');

Exporting is handled using CommonJS module syntax:

MyClass.js
module.exports = class MyClass
{
    ...
};

Further, using registered file handlers, the application can automatically handle each file type differently: a class will be handled one way whereas a Vue component will be handled another and JSON files yet another.

// No file extension, assumed to be a class.
const MyClass = use('MyNamespace/MyClass');

// Load and parse a VueJS single-file component.
const MyComponent = use('MyNamespace/MyComponent.vue');

// Load and parse a JSON data file.
const myData = use('MyNamespace/myData.json');

Last updated