Theming and Localization of Wijmo Controls in Vue Applications
Vue is a JavaScript application framework similar to Angular and React, but considerably lighter. Despite its tiny footprint, Vue is a powerful and flexible framework. Wijmo's library of JavaScript UI Components is also compact, robust, and flexible. The two libraries are a great match.
Wijmo features over 40 locales, referred to as cultures, and 25 different built-in themes. By default, Wijmo formats and parses data using the American English culture. If your application targets other cultures, include references to the appropriate Wijmo culture files.
Below we'll create a sample Vue application with Wijmo controls. Then we will outline how to change Wijmo themes and cultures in a Vue application using both static and dynamic methods.
Creating a Sample Vue Application with Wijmo Controls
Use Vue CLI to create a project with default settings:
Details about creating a Vue CLI application creation can be found here.
Add Wijmo to the project:
How to Modify the Vue CLI Project
- Create the file "src/data.js" which forms random sample data for Wijmo controls:
- Create the component "src/components/WijmoSample.vue" file which contains several Wijmo controls with random sample data:
- Modify the standard Vue template (src/App.vue) file by replacing the unnecessary component with the previously created WijmoSample component:
-
Remove unused "src/assets" folder and "src/components/HelloWorld.vue" file
-
Run the sample application.
yarn serve
- Navigate to http://localhost:8080/ in the browser. View the Wijmo controls with tne default theme and locale.
Use this sample application as a base for the demonstration of static and dynamic methods.
Using the Static Method in Vue Applications
Import the required css or js files to statically use a built-in theme or locale.
Built-in themes are located in themes folder of @grapecity/wijmo.styles module.
- Apply a built-in theme: modify src/components/WijmoSample.vue file by replacing import of default them by importe of another buil-in them. For example, to apply organic theme:
Built-in locales are located in @grapecity/wijmo.cultures
module.
- Apply built-in locale: import the appropriate js-file after loading Wijmo. For example, to load the Japanese locale add the next line as the last line among imports:
As a result <script> import"
part of _src/components/WijmoSample.vue
file looks like:
Here's what the application should look:
Using the Dynamic Method in Vue Applications
Dynamic theming and localization require:
- Publishing resources (themes and locales)
- Loading required ones on the fly (for example, on changing of theme or locale selectors)
Resource Publication
The most efficient resource publication method is webpack configuration of Vue project. We configure webpack to achieve next objectives:
- Resources automatically copied from GrapeCity modules to their specified subfolder of dist folder on every build
- Subfolder names aren't hard coded (can be easily changed in the future)
- The application works when deployed in non-root
- List of available themes and locales dynamically formed in accordance with the content of appropriate folders
To achieve these objectives, we create vue.config.js file in the root of the project with the content:
Dynamic Resource Loading
Dynamically add one of these elements (<link>
for css, <script>
for js) to HTML in the document head as follows:
- css-file (id attribute added to identify previously added element for removal):
- Js-file:
Loading a theme or locale requires an extra step to re-render the Wijmo controls. Re-render by using the invalidateAll static method of the Control Wijmo base class. The invalidateAll method should be called in the onload event of the added <link>
or <script>
element.
To simplify the manipulation of the elements, we create a universal function that handles the removal of the previous element and adds the new one.
Use this function as the WijmoSample component method:
We use "_process.env.BASE_URL"_ value for cases when deploying the application in a non-root folder of the webserver.
Now resources may be loaded by calling "this.addResource" method:
Names of public folders of themes and locales resources are accessable in _process.env.WIJMO_THEMES_PUBLIC_FOLDER_ and _process.env.WIJMO_CULTURES_PUBLIC_FOLDER_ variables, respectively (in accordance with the earlier defined webpack configuration in vue.config.js file).
Finally, we add two Wijmo ComboBoxes from which we select a theme and locale. Take the items for selectors from the "process.env" variable (see "vue.config.js" file). Event handlers apply default values for theme and locale to each selector.
The resulting WijmoSample code ("src/components/WijmoSample.vue" file) should look like:
The resulting application:
The application theme or locale changes with every change of the corresponding selector value.
Thank you for reading. Let us know how this information helps with your applications.
Read more about creating a Vue project.
Download the source code | Live demo
Theming and localization of a Vue application based on Wijmo components are both rather simple. There are two methods for applying of build-in themes and locales: static and dynamic.
A static method is straightforward and maybe applicable when the required theme and locale are both known in advance.
The dynamic method is more complicated but permits the creation of applications that can change its appearance and culture-dependent formatting on the fly.
Happy coding! If you have questions or comments be sure to enter them below.