Welly Wellington
Welly Wellington

Reputation: 51

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"
  ]
}

Upvotes: 0

Views: 8415

Answers (3)

Yom T.
Yom T.

Reputation: 9180

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;
    });
  }
}

Upvotes: 3

Hardik Raval
Hardik Raval

Reputation: 3641

You can also use vue-lodash package -- Follow these steps:

  1. npm install --save vue-lodash
  2. in main.js -- import VueLodash from 'vue-lodash'
  3. in main.js after import -- Vue.use(VueLodash)

Usage:

Vue._.random(20);
this._.random(20);

-------- OR ------------

In your main.js add this line of code:

window._ = require('lodash');

That way it will work without Vue or this:

Just do -- _.map()

Upvotes: 1

Sukanta Bala
Sukanta Bala

Reputation: 879

You can import lodash in your main.js file by javascript window object like this:

window._ = require('lodash');

Then use it anywhere in your projet like this:

var original = [
  { label: 'private', value: '[email protected]' },
  { label: 'work', value: '[email protected]' }
];

var update = [
  { label: 'private', value: '[email protected]' },
  { label: 'school', value: '[email protected]' }
];

var result = _.unionBy(update, original);
var sortedresult = _map(_.sortBy(result, 'label'));
console.log(sortedresult);

I just use lodash unionBy and sortBy method for example.

Upvotes: 0

Related Questions