Using lodash in all of vue component template
up vote
31
down vote
favorite
Can I using lodash _
in all of my vue component?
for example:
I have components organized like below:
App.vue
> Parent.vue
> Child.vue
I would like to all of my component can access _
lodash without defined in every component vm data
===
I am also trying using Mixins. it works. but the result not expected like this _().isEmpty()
instead of _.isEmpty()
javascript lodash vue.js vue-component
add a comment |
up vote
31
down vote
favorite
Can I using lodash _
in all of my vue component?
for example:
I have components organized like below:
App.vue
> Parent.vue
> Child.vue
I would like to all of my component can access _
lodash without defined in every component vm data
===
I am also trying using Mixins. it works. but the result not expected like this _().isEmpty()
instead of _.isEmpty()
javascript lodash vue.js vue-component
2
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to writeimport lodash
everywhere.
– Zhang Buzz
Jan 18 at 8:47
add a comment |
up vote
31
down vote
favorite
up vote
31
down vote
favorite
Can I using lodash _
in all of my vue component?
for example:
I have components organized like below:
App.vue
> Parent.vue
> Child.vue
I would like to all of my component can access _
lodash without defined in every component vm data
===
I am also trying using Mixins. it works. but the result not expected like this _().isEmpty()
instead of _.isEmpty()
javascript lodash vue.js vue-component
Can I using lodash _
in all of my vue component?
for example:
I have components organized like below:
App.vue
> Parent.vue
> Child.vue
I would like to all of my component can access _
lodash without defined in every component vm data
===
I am also trying using Mixins. it works. but the result not expected like this _().isEmpty()
instead of _.isEmpty()
javascript lodash vue.js vue-component
javascript lodash vue.js vue-component
edited Jun 8 '16 at 8:03
asked Jun 8 '16 at 5:51
antoniputra
1,39841424
1,39841424
2
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to writeimport lodash
everywhere.
– Zhang Buzz
Jan 18 at 8:47
add a comment |
2
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to writeimport lodash
everywhere.
– Zhang Buzz
Jan 18 at 8:47
2
2
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write
import lodash
everywhere.– Zhang Buzz
Jan 18 at 8:47
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write
import lodash
everywhere.– Zhang Buzz
Jan 18 at 8:47
add a comment |
9 Answers
9
active
oldest
votes
up vote
46
down vote
accepted
Some of the current answers may work in your scenario, but they have downsides:
- Adding to the
window
object means your Vue project can't be server rendered, because servers don't have access to thewindow
object. - Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.
An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this
keyword.
import _ from 'lodash';
Object.defineProperty(Vue.prototype, '$_', { value: _ });
Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:
export default {
created() {
console.log(this.$_.isEmpty(null));
}
}
The advantage of using Object.defineProperty
rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.
This is more thoroughly explained in this blog post (which I wrote).
Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash";
at the top of the file requiring it.
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
add a comment |
up vote
18
down vote
You could import the lodash
into each component:
<script>
import _ from 'lodash'
export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>
the example of your code is only wrappingisEmpty
method. how about the other lodash methods?
– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace theisEmpty
with anylodash
method.
– Pantelis Peslis
Jun 8 '16 at 8:21
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type inimport lodash
everywhere. That's not good practice.
– Zhang Buzz
Jan 18 at 8:50
add a comment |
up vote
12
down vote
For inline templates separated from the js module code it should work with:
Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});
And then you can use it in template in "native" way - _.isEmpty(value)
.
add a comment |
up vote
12
down vote
You could import lodash
globally like this
window._ = require('lodash');
Once that has been imported, you will have access to _
from anywhere.
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
Also do not forget to add below code into.eslintrc.js
file, otherwise ESLint will always whine about_ is undefined
.globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
add a comment |
up vote
7
down vote
A simple approach that worked for me:
Vue.set(Vue.prototype, '_', _);
This should allow you to use _ in all component templates and vue instances.
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
add a comment |
up vote
3
down vote
You can use plugin/mixin like this.
import _ from 'lodash';
exports default {
install : function(Vue, options){
Vue.mixin({
computed : {
"_" : function(){
return _;
}
}
});
}
}
add a comment |
up vote
1
down vote
Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.
I would note that in the tutorial, he left import "jquery"
in his app.js file, which is not required. The plugin with import it automatically.
add a comment |
up vote
0
down vote
Check out vue-lodash!!
It's a new wrapper for using lodash in vue.
You can call it using
Vue._.random(20) // for getting random number between 20
this._.random(20) //or other method you want to use
in any of the component file :)
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
add a comment |
up vote
0
down vote
import _ from 'lodash'
Vue.prototype._ = _
Insert these lines in your main.js file and it will work all over your app.
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
46
down vote
accepted
Some of the current answers may work in your scenario, but they have downsides:
- Adding to the
window
object means your Vue project can't be server rendered, because servers don't have access to thewindow
object. - Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.
An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this
keyword.
import _ from 'lodash';
Object.defineProperty(Vue.prototype, '$_', { value: _ });
Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:
export default {
created() {
console.log(this.$_.isEmpty(null));
}
}
The advantage of using Object.defineProperty
rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.
This is more thoroughly explained in this blog post (which I wrote).
Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash";
at the top of the file requiring it.
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
add a comment |
up vote
46
down vote
accepted
Some of the current answers may work in your scenario, but they have downsides:
- Adding to the
window
object means your Vue project can't be server rendered, because servers don't have access to thewindow
object. - Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.
An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this
keyword.
import _ from 'lodash';
Object.defineProperty(Vue.prototype, '$_', { value: _ });
Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:
export default {
created() {
console.log(this.$_.isEmpty(null));
}
}
The advantage of using Object.defineProperty
rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.
This is more thoroughly explained in this blog post (which I wrote).
Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash";
at the top of the file requiring it.
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
add a comment |
up vote
46
down vote
accepted
up vote
46
down vote
accepted
Some of the current answers may work in your scenario, but they have downsides:
- Adding to the
window
object means your Vue project can't be server rendered, because servers don't have access to thewindow
object. - Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.
An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this
keyword.
import _ from 'lodash';
Object.defineProperty(Vue.prototype, '$_', { value: _ });
Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:
export default {
created() {
console.log(this.$_.isEmpty(null));
}
}
The advantage of using Object.defineProperty
rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.
This is more thoroughly explained in this blog post (which I wrote).
Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash";
at the top of the file requiring it.
Some of the current answers may work in your scenario, but they have downsides:
- Adding to the
window
object means your Vue project can't be server rendered, because servers don't have access to thewindow
object. - Importing in every file works fine, but it can be a pain if you have to remember to do it in every file.
An alternative approach is to add your library to the Vue prototype. All components inherit from this so they will now all be able to access your library from the this
keyword.
import _ from 'lodash';
Object.defineProperty(Vue.prototype, '$_', { value: _ });
Now lodash is available as an instance method for all components. In a .vue file you can do this without importing anything:
export default {
created() {
console.log(this.$_.isEmpty(null));
}
}
The advantage of using Object.defineProperty
rather than a normal property assignment is that you can define a descriptor which allows you to make the property read-only, which it will be by default. This stops consuming components from overwriting it.
This is more thoroughly explained in this blog post (which I wrote).
Note: The downside to this approach is that you get the entire Lodash library, even if you only need one or two functions. If that's a problem, best to use import { reduce, whatever } from "lodash";
at the top of the file requiring it.
edited Aug 4 at 7:45
answered Apr 22 '17 at 6:58
anthonygore
2,6812026
2,6812026
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
add a comment |
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
8
8
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
FYI - can write like Vue.prototype.$lodash = lodash
– DogCoffee
May 5 '17 at 9:18
Thank you! I aliased lodash with
$_
– pymarco
Sep 6 '17 at 18:21
Thank you! I aliased lodash with
$_
– pymarco
Sep 6 '17 at 18:21
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
I've added my solution below using the Webpack ProvidePlugin, which I believe is an even cleaner and simpler way of doing things.
– user5814
Mar 31 at 10:08
2
2
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
I don't like Vue.prototype.$lodash = lodash as much as it can be overwritten. By default, values added using Object.defineProperty() are immutable.
– anthonygore
Apr 1 at 10:31
add a comment |
up vote
18
down vote
You could import the lodash
into each component:
<script>
import _ from 'lodash'
export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>
the example of your code is only wrappingisEmpty
method. how about the other lodash methods?
– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace theisEmpty
with anylodash
method.
– Pantelis Peslis
Jun 8 '16 at 8:21
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type inimport lodash
everywhere. That's not good practice.
– Zhang Buzz
Jan 18 at 8:50
add a comment |
up vote
18
down vote
You could import the lodash
into each component:
<script>
import _ from 'lodash'
export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>
the example of your code is only wrappingisEmpty
method. how about the other lodash methods?
– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace theisEmpty
with anylodash
method.
– Pantelis Peslis
Jun 8 '16 at 8:21
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type inimport lodash
everywhere. That's not good practice.
– Zhang Buzz
Jan 18 at 8:50
add a comment |
up vote
18
down vote
up vote
18
down vote
You could import the lodash
into each component:
<script>
import _ from 'lodash'
export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>
You could import the lodash
into each component:
<script>
import _ from 'lodash'
export default {
methods: {
test (value) {
return _.isEmpty(value)
}
}
}
</script>
answered Jun 8 '16 at 6:10
Pantelis Peslis
8,43832840
8,43832840
the example of your code is only wrappingisEmpty
method. how about the other lodash methods?
– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace theisEmpty
with anylodash
method.
– Pantelis Peslis
Jun 8 '16 at 8:21
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type inimport lodash
everywhere. That's not good practice.
– Zhang Buzz
Jan 18 at 8:50
add a comment |
the example of your code is only wrappingisEmpty
method. how about the other lodash methods?
– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace theisEmpty
with anylodash
method.
– Pantelis Peslis
Jun 8 '16 at 8:21
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type inimport lodash
everywhere. That's not good practice.
– Zhang Buzz
Jan 18 at 8:50
the example of your code is only wrapping
isEmpty
method. how about the other lodash methods?– antoniputra
Jun 8 '16 at 8:04
the example of your code is only wrapping
isEmpty
method. how about the other lodash methods?– antoniputra
Jun 8 '16 at 8:04
This is just an example based on yours. You could replace the
isEmpty
with any lodash
method.– Pantelis Peslis
Jun 8 '16 at 8:21
This is just an example based on yours. You could replace the
isEmpty
with any lodash
method.– Pantelis Peslis
Jun 8 '16 at 8:21
2
2
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
using import is the right way if you using lodash (_) in child component.
– hendra1
Feb 14 '17 at 9:38
I downvote because we have thousands of vue files, and we don't want to repeat type in
import lodash
everywhere. That's not good practice.– Zhang Buzz
Jan 18 at 8:50
I downvote because we have thousands of vue files, and we don't want to repeat type in
import lodash
everywhere. That's not good practice.– Zhang Buzz
Jan 18 at 8:50
add a comment |
up vote
12
down vote
For inline templates separated from the js module code it should work with:
Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});
And then you can use it in template in "native" way - _.isEmpty(value)
.
add a comment |
up vote
12
down vote
For inline templates separated from the js module code it should work with:
Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});
And then you can use it in template in "native" way - _.isEmpty(value)
.
add a comment |
up vote
12
down vote
up vote
12
down vote
For inline templates separated from the js module code it should work with:
Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});
And then you can use it in template in "native" way - _.isEmpty(value)
.
For inline templates separated from the js module code it should work with:
Vue.component('some-tag', {
computed: {
_() {
return _;
}
}
});
And then you can use it in template in "native" way - _.isEmpty(value)
.
answered Aug 18 '16 at 20:34
antongorodezkiy
1,42022030
1,42022030
add a comment |
add a comment |
up vote
12
down vote
You could import lodash
globally like this
window._ = require('lodash');
Once that has been imported, you will have access to _
from anywhere.
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
Also do not forget to add below code into.eslintrc.js
file, otherwise ESLint will always whine about_ is undefined
.globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
add a comment |
up vote
12
down vote
You could import lodash
globally like this
window._ = require('lodash');
Once that has been imported, you will have access to _
from anywhere.
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
Also do not forget to add below code into.eslintrc.js
file, otherwise ESLint will always whine about_ is undefined
.globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
add a comment |
up vote
12
down vote
up vote
12
down vote
You could import lodash
globally like this
window._ = require('lodash');
Once that has been imported, you will have access to _
from anywhere.
You could import lodash
globally like this
window._ = require('lodash');
Once that has been imported, you will have access to _
from anywhere.
answered Oct 11 '16 at 17:02
Brandon Ferens
12912
12912
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
Also do not forget to add below code into.eslintrc.js
file, otherwise ESLint will always whine about_ is undefined
.globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
add a comment |
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
Also do not forget to add below code into.eslintrc.js
file, otherwise ESLint will always whine about_ is undefined
.globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
3
3
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
I downvoted because window is undefined when you server render the app.
– anthonygore
Apr 22 '17 at 7:04
1
1
Also do not forget to add below code into
.eslintrc.js
file, otherwise ESLint will always whine about _ is undefined
. globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
Also do not forget to add below code into
.eslintrc.js
file, otherwise ESLint will always whine about _ is undefined
. globals: { _: false, },
– Zhang Buzz
Jan 18 at 9:13
add a comment |
up vote
7
down vote
A simple approach that worked for me:
Vue.set(Vue.prototype, '_', _);
This should allow you to use _ in all component templates and vue instances.
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
add a comment |
up vote
7
down vote
A simple approach that worked for me:
Vue.set(Vue.prototype, '_', _);
This should allow you to use _ in all component templates and vue instances.
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
add a comment |
up vote
7
down vote
up vote
7
down vote
A simple approach that worked for me:
Vue.set(Vue.prototype, '_', _);
This should allow you to use _ in all component templates and vue instances.
A simple approach that worked for me:
Vue.set(Vue.prototype, '_', _);
This should allow you to use _ in all component templates and vue instances.
answered Aug 28 '17 at 19:03
newms87
5031618
5031618
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
add a comment |
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
It doesn't work..
– Rbex
Jun 9 at 8:06
It doesn't work..
– Rbex
Jun 9 at 8:06
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
this solution just works for one of my projects, not in another one, don't know why... weird.
– spetsnaz
Jul 11 at 21:13
add a comment |
up vote
3
down vote
You can use plugin/mixin like this.
import _ from 'lodash';
exports default {
install : function(Vue, options){
Vue.mixin({
computed : {
"_" : function(){
return _;
}
}
});
}
}
add a comment |
up vote
3
down vote
You can use plugin/mixin like this.
import _ from 'lodash';
exports default {
install : function(Vue, options){
Vue.mixin({
computed : {
"_" : function(){
return _;
}
}
});
}
}
add a comment |
up vote
3
down vote
up vote
3
down vote
You can use plugin/mixin like this.
import _ from 'lodash';
exports default {
install : function(Vue, options){
Vue.mixin({
computed : {
"_" : function(){
return _;
}
}
});
}
}
You can use plugin/mixin like this.
import _ from 'lodash';
exports default {
install : function(Vue, options){
Vue.mixin({
computed : {
"_" : function(){
return _;
}
}
});
}
}
answered Mar 17 '17 at 6:15
Hotbrains
311
311
add a comment |
add a comment |
up vote
1
down vote
Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.
I would note that in the tutorial, he left import "jquery"
in his app.js file, which is not required. The plugin with import it automatically.
add a comment |
up vote
1
down vote
Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.
I would note that in the tutorial, he left import "jquery"
in his app.js file, which is not required. The plugin with import it automatically.
add a comment |
up vote
1
down vote
up vote
1
down vote
Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.
I would note that in the tutorial, he left import "jquery"
in his app.js file, which is not required. The plugin with import it automatically.
Bit late to the party but through my research of finding a way to import lodash and other libraries into all my .vue files, I encountered the webpack ProvidePlugin, which achieves everything the OP requested with almost no fuss. To implement this solution, following this fantastic tutorial.
I would note that in the tutorial, he left import "jquery"
in his app.js file, which is not required. The plugin with import it automatically.
answered Mar 31 at 5:51
user5814
1,058519
1,058519
add a comment |
add a comment |
up vote
0
down vote
Check out vue-lodash!!
It's a new wrapper for using lodash in vue.
You can call it using
Vue._.random(20) // for getting random number between 20
this._.random(20) //or other method you want to use
in any of the component file :)
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
add a comment |
up vote
0
down vote
Check out vue-lodash!!
It's a new wrapper for using lodash in vue.
You can call it using
Vue._.random(20) // for getting random number between 20
this._.random(20) //or other method you want to use
in any of the component file :)
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
add a comment |
up vote
0
down vote
up vote
0
down vote
Check out vue-lodash!!
It's a new wrapper for using lodash in vue.
You can call it using
Vue._.random(20) // for getting random number between 20
this._.random(20) //or other method you want to use
in any of the component file :)
Check out vue-lodash!!
It's a new wrapper for using lodash in vue.
You can call it using
Vue._.random(20) // for getting random number between 20
this._.random(20) //or other method you want to use
in any of the component file :)
answered Apr 22 '17 at 6:22
Ewing
711
711
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
add a comment |
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
5
5
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
This works, but do you really need another module in your project to add something that can be added manually with one line?
– anthonygore
Apr 22 '17 at 7:07
1
1
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
I think you are making a good point, but it depends on person. Cuz some people might wanna use it often in a big project that contains lots of .vue file, but some might only use it for one .vue component. Just like the idea of having vue-axios. It's not necessary but it's definitely convenient in some cases! :)
– Ewing
Apr 24 '17 at 21:41
add a comment |
up vote
0
down vote
import _ from 'lodash'
Vue.prototype._ = _
Insert these lines in your main.js file and it will work all over your app.
add a comment |
up vote
0
down vote
import _ from 'lodash'
Vue.prototype._ = _
Insert these lines in your main.js file and it will work all over your app.
add a comment |
up vote
0
down vote
up vote
0
down vote
import _ from 'lodash'
Vue.prototype._ = _
Insert these lines in your main.js file and it will work all over your app.
import _ from 'lodash'
Vue.prototype._ = _
Insert these lines in your main.js file and it will work all over your app.
answered Nov 22 at 16:23
butaminas
3018
3018
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37694243%2fusing-lodash-in-all-of-vue-component-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It's a best practice to include any required modules within the component
– Jeff
Jun 8 '16 at 6:06
Could you be more concise? Where exactly do you want to use underscore? within your templates or just in your methods / component logic? If you want to reduce redundancy it seems okey to me if you append a stateless library like underscore JS to the global namespace.
– Franz Skuffka
Jun 8 '16 at 6:14
@Jeff is there any way to reusable it?
– antoniputra
Jun 8 '16 at 8:01
@FranzSkuffka I want use underscore in both of component template and logic.
– antoniputra
Jun 8 '16 at 8:01
I don't agree. Very common modules like Lodash should be imported globally, so we do not need to write
import lodash
everywhere.– Zhang Buzz
Jan 18 at 8:47