Search code examples
vue.jslodash

Why is lodash not working when I import it in Vue.js


I created a fresh new install of vue.js using "vue create todo --default" command. After that I installed lodash too with this command "npm i --save lodash". I can see it in my package.json on the "dependencies" object. The problem is that when I import it on my main.js and use the lodash functions, it is showing the error "_ is not defined". So I tried importing it inside the App.vue. The error "_ is not defined" was removed but it is not working.

Here are the code inside the App.vue, main.js, and package.json

main.js

import Vue from 'vue'
import App from './App.vue'
import "bootstrap/dist/css/bootstrap.min.css";
import "jquery/dist/jquery";
import "bootstrap/dist/js/bootstrap.min";

import _ from "lodash";
Vue.prototype._ = _;

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

App.vue

<template>
    <div id="app">
        <h4 class="bg-primary text-white text-center p-2">
            {{name}}'s' To Do List
        </h4>
        <div class="container-fluid p-4">
            <div class="row">
                <div class="col font-weight-bold">Task</div>
                <div class="col-2 font-weight-bold">Done</div>
            </div>
            <div class="row" v-for="t in completedtask" v-bind:key="t.action">
                <div class="col">{{t.action}}</div>
                <div class="col-2">
                    <input type="checkbox" v-model="t.done" class="form-check-input">
                    {{t.done}}
                </div>
            </div>
        </div>
    </div>
</template>

<script>

export default {
    data(){
        return{
            name: "Welly",
            tasks: [{
                    action: "Buy Flowers",
                    done: false
                },
                {
                    action: "Get Shoes",
                    done: false
                },
                {
                    action: "Collect Tickets",
                    done: true
                },
                {
                    action: "Call Joe",
                    done: false
                }
            ]
        };
    },
    computed: {
        hidecompletedtask(){
            return _.map(this.tasks,(val)=>{
                return !val.done;
            });
        }
    }
}
</script>

<style>
</style>

package.json

{
  "name": "todo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "bootstrap": "^4.4.1",
    "core-js": "^3.4.4",
    "jquery": "^3.4.1",
    "lodash": "^4.17.15",
    "popper.js": "^1.16.1",
    "vue": "^2.6.10"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^4.1.0",
    "@vue/cli-plugin-eslint": "^4.1.0",
    "@vue/cli-service": "^4.1.0",
    "babel-eslint": "^10.0.3",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "vue-template-compiler": "^2.6.10"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ]
}

Solution

  • You'll still need to access the prototype via the this context, like this._.map().

    computed: {
      hidecompletedtask() {
        return this._.map(this.tasks, (val) => {
          return !val.done;
        });
      }
    }
    

    Reference: Adding Instance Properties.


    Alternatively, you could extend the global window object. Put the following line in your main.js (or some booting file).

    window._ = require('lodash');
    

    Somewhere else where you need the library:

    computed: {
      hidecompletedtask() {
        // The underscore (_) character now refers to the `window._ object`
        // so you can drop the `this`.
        return _.map(this.tasks, (val) => {
          return !val.done;
        });
      }
    }