Open In App

How to Use Lodash in Vue Component Template?

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In a Vue component, you can use Lodash by importing specific methods or the entire library into your script section. Lodash methods can be applied in computed properties or methods for efficient data manipulation, helping to keep your Vue templates clean and optimized.

Prerequisites

Approach

  • The products array contains product objects with name and price properties, defined in the data() section of the Vue component.
  • In Vue 2, a global cheapProducts filter is applied directly in the template using v-for="(product, index) in products | cheapProducts", which filters out expensive products.
  • The filtered list of products is rendered in the ul element, displaying only products with prices less than 50.
  • Each product in the v-for loop is assigned a unique key (index) to ensure Vue optimally tracks the changes.

Steps to use Lodash in vue Component Template

Step 1: Create or Navigate to Your Vue Project

npm install -g @vue/cli
vue create my-Step 1: vue-app
cd my-vue-app

Step 2: Navigate to your Vue project directory and install Lodash using npm

npm install lodash

Updated Dependencies:

"dependencies": {
"core-js": "^3.8.3",
"lodash": "^4.17.21",
"vue": "^3.0.0"
}

Project Structure:

file
Folder Structure

Step 3: Locate the Component File

  • Now, open your Vue project and navigate to the component where you want to use Lodash. Typically, you'll find components in the src/components folder, such as HelloWorld.vue, but you can also create a new one.

Step 4: Add the Lodash Code

  • Edit the Vue component (e.g., HelloWorld.vue), adding Lodash as needed.

Example: This example shows the use of Lodash.

JavaScript
// src/components/HelloWorld.vue

<template>
  <div>
    <h1>Cheap Products List</h1>
    <ul>
      <!-- Apply the 'cheapProducts' filter as a method -->
      <li v-for="(product, index) in getCheapProducts(products)" :key="index">
        {{ product.name }} - ${{ product.price }}
      </li>
    </ul>
  </div>
</template>

<script>
import _ from 'lodash';

export default {
  data() {
    return {
      products: [
        { name: 'Product 1', price: 30 },
        { name: 'Product 2', price: 60 },
        { name: 'Product 3', price: 45 },
        { name: 'Product 4', price: 100 }
      ]
    };
  },
  methods: {
    // Filter products using Lodash _.filter method
    getCheapProducts(products) {
      return _.filter(products, (product) => product.price > 50);
    }
  }
};
</script>

<style scoped>
/* Your styles here */
</style>
JavaScript
<!-- src/App.vue -->
<template>
  <div id="app">
    <HelloWorld />
  </div>
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue';

export default {
  name: 'App',
  components: {
    HelloWorld,
  },
};
</script>
JavaScript
// main.js
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

Step 5: Run the Project

Once you've added the code, you can run your project with the following command:

npm run serve

Output:


Next Article

Similar Reads