DevExtreme Vue - Create a DevExtreme application with Vite.js
Vite (pronounced "veet") is a web development framework from the creators of Vue.js. It offers an intuitive API, fast build times, and efficient dependency bundling thanks to its use of native ESM modules.
Unlike its major competitors — Next.js and create-react-app
— Vite is compatible with a variety of front-end solutions. In addition to its original companion framework — Vue.js — you can use Vite with React.js, Svelte, or even vanilla JavaScript.
In this tutorial, we'll create a new Vite application with a Vue.js frontend. The application will pull data from the publicly available Cat Facts API and display it with the DevExpress DataGrid.
1. Create a new Vite project
Use the following command to launch the Vite project wizard:
npm create vite@latest
Answer the wizard's prompts to select your project name, your front-end framework, and your programming language (JavaScript or TypeScript).
The Vite wizard does not install project dependencies automatically. Enter your project directory and run npm install
:
cd vite-project; npm install
To start the development server, execute the following command:
npm run dev
2. Configure DevExtreme dependencies
Add the following dependencies to the package.json
file:
"dependencies": { "devextreme": "^24.2", "devextreme-vue": "^24.2", ... }
Install the new dependencies:
npm install
3. Remove unnecessary code
Open the
srtc/App.vue
file, and replace its content with the following two tags:App.vue<template> </template> <script setup lang="ts"> </script>
Remove the
src/components/HelloWorld.vue
file because we no longer need it.The
src/style.css
file includes CSS code that overrides DevExtreme styles. Open the file and remove its contents.
4. Set up a DevExtreme theme
Add the following
import
statement to thesrc/main.ts
file:main.tsimport 'devextreme/dist/css/dx.fluent.blue.light.css';
This statement applies the
fluent
theme to your application. You can select a different DevExtreme theme if you wish.To apply this theme throughout your application, edit the
index.html
file in the project's root directory. Add thedx-viewport
class to thebody
tag:index.html<body class="dx-viewport">
5. Add a new component
Create a new file for the component ---
src/components/CatFactGrid.vue
. Populate it with the following two tags:CatFactGrid.vue<template> // component declaration </template> <script setup lang="ts"> // component setup </script>
Modify the
src/App.vue
file to display our new component:App.vue<template> <CatFactGrid /> </template> <script setup lang="ts"> import CatFactGrid from './components/CatFactGrid.vue' </script>
6. Configure the DataGrid
Go back to the CatFactGrid.vue
file. The component is currently empty. Use the script
tag to set up a DevExtreme DataGrid:
<script setup lang="ts"> import { DxDataGrid } from "devextreme-vue/data-grid"; // Import the DevExtreme DataGrid component import CustomStore from "devextreme/data/custom_store"; // Import the CustomStore object const catFactStore = new CustomStore({ // Define a CustomStore that pulls data from the Cat Facts API load: () => { return fetch("https://catfact.ninja/facts") .then(handleErrors) .then((response) => response.json()) .catch(() => { throw "Network error"; }); }, }); function handleErrors(response: Response) { // Handle network request failure if (!response.ok) { throw Error(response.statusText); } return response; } </script>
Use the template
tag to initialize the component, and attach it to the aforementioned CustomStore
:
<template> <DxDataGrid id="data-grid" :data-source="catFactStore" // Load the CustomStore from the script :row-alternation-enabled="true" // Apply a gray background to even rows for greater visibility :show-column-lines="true" // Display column borders > </DxDataGrid> </template>
7. View the results
You do not need to relaunch the development server to apply these changes. Just refresh the page and see the grid in action:
8. Next Steps
DevExtreme components are powerful, easy to use, and fully compatible with Vite. View our application templates and demos to discover the full capabilities of DevExtreme components.
Read the Vite.js documentation for more information on the Vite framework.
If you have technical questions, please create a support ticket in the DevExpress Support Center.