admin管理员组文章数量:1424458
Let's say that I have a script file, foo.js
:
function doStuff() {
// how to access store and other plugins here?
}
export default { doStuff }
Without passing the calling ponent as an argument, how can I access things like app
or installed plugins like store
, i18n
in a script file like the one above?
Let's say that I have a script file, foo.js
:
function doStuff() {
// how to access store and other plugins here?
}
export default { doStuff }
Without passing the calling ponent as an argument, how can I access things like app
or installed plugins like store
, i18n
in a script file like the one above?
-
Please, elaborate why default plugin format doesn't work for you?
export default ({ app, store }) => { /* plugin code */ }
– aBiscuit Commented Nov 14, 2018 at 15:33 - @aBiscuit Because the script file is not a plugin – Johan Commented Nov 14, 2018 at 15:41
-
Where does
doStuff
is expected to be called from? Components, store or other places? This may help to determine better approach of implementation. – aBiscuit Commented Nov 14, 2018 at 17:00 -
@aBiscuit Sorry for not being clear about that. Mainly from ponents. I would like to avoid
doStuff(this)
,doStuff.call(this)
etc. – Johan Commented Nov 14, 2018 at 18:56
1 Answer
Reset to default 5There are multiple ways to call custom function with this
being a reference to the ponent it was invoked in.
1) Using mixins
Custom function can be declared as a method and used within ponent via this.customMethod
.
customHelpers.js
const customHelpers = {
methods: {
doStuff () {
// this will be referenced to ponent it is executed in
}
}
}
ponent.vue
// ponent.vue
import customHelpers from '~/mixins/customHelpers'
export default {
mixins: [customHelpers],
mounted () {
this.doStuff()
}
}
2. Using context injection
Declare custom plugin:
plugins/customHelpers.js
import Vue from 'vue'
Vue.prototype.$doStuff = () => { /* stuff happens here */ }
And use plugin in nuxt.config.js
export default {
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']
}
This makes method available inside every ponent:
export default {
mounted () {
this.$doStuff()
}
}
3) Using bined injection
Same as method 2, but injection will be also accessible inside fetch
, asyncData
and inside store modules. Bindings to this
may vary, since context is not available everywhere.
plugins/customHelpers.js
export default ({ app }, inject) => {
inject('doStuff', () => { /* stuff happens here */ })
}
And use plugin in nuxt.config.js
export default {
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']
}
Usage example:
export default {
asyncData ({ app }) {
app.$doStuff()
}
}
Please, refer to documentation for more examples.
本文标签: javascriptAccess Nuxt plugins in js filesStack Overflow
版权声明:本文标题:javascript - Access Nuxt plugins in .js files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745356049a2655058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论