admin管理员组文章数量:1022137
I'm using Nuxt JS's inject
function to inject a function into my page for code reusability. I'd like to use the function in another plugin file and can't seem to get the function in there.
Here's my set up:
- plugins/utils/tracking.js
function setCookiePrefix () {
return 'fudge__'
}
function getCookie (app, name) {
try {
const prefix = setCookiePrefix()
const cookie = app.$cookies.get(`${prefix}${name}`)
if (!cookie || cookie == '') {
throw 'cookie not set'
}
return cookie
} catch (err) { }
return null
}
export default function ({ app, store, context }, inject) {
/*
* Get just the affiliate (cpm_id OR affiliate)
* examples: "my_brand", "blah"
*/
inject('getAffiliate', () => {
const affiliate = getCookie(app, 'affiliate')
const brand = getBrand(store)
if (!affiliate) return brand
return affiliate
})
}
And the file I'm trying to utilise the getAffiliate
function in from my tracking.js file:
- plugins/init-brand.js
export default async function ({ app, route, store }) {
const affiliate = app.$getAffiliate()
console.log(affiliate) <-- undefined
}
I've tried:
app.$getAffiliate()
this.$getAffiliate()
<-- this works in a Vue file$getAffiliate()
this.getAffiliate()
What am I missing to access my getAffiliate
function in another plugin file?
I'm using Nuxt JS's inject
function to inject a function into my page for code reusability. I'd like to use the function in another plugin file and can't seem to get the function in there.
Here's my set up:
- plugins/utils/tracking.js
function setCookiePrefix () {
return 'fudge__'
}
function getCookie (app, name) {
try {
const prefix = setCookiePrefix()
const cookie = app.$cookies.get(`${prefix}${name}`)
if (!cookie || cookie == '') {
throw 'cookie not set'
}
return cookie
} catch (err) { }
return null
}
export default function ({ app, store, context }, inject) {
/*
* Get just the affiliate (cpm_id OR affiliate)
* examples: "my_brand", "blah"
*/
inject('getAffiliate', () => {
const affiliate = getCookie(app, 'affiliate')
const brand = getBrand(store)
if (!affiliate) return brand
return affiliate
})
}
And the file I'm trying to utilise the getAffiliate
function in from my tracking.js file:
- plugins/init-brand.js
export default async function ({ app, route, store }) {
const affiliate = app.$getAffiliate()
console.log(affiliate) <-- undefined
}
I've tried:
app.$getAffiliate()
this.$getAffiliate()
<-- this works in a Vue file$getAffiliate()
this.getAffiliate()
What am I missing to access my getAffiliate
function in another plugin file?
-
1
Have you connected the plugins in the configuration in the right order? The plugin with the inject function should be higher in the
plugins: []
innuxt.config.js
to be available in the second plugin file – JanuszO Commented Apr 16, 2022 at 8:37
1 Answer
Reset to default 3I have an api plugin with some axios set up. I can load it inside another plugin file like this:
export default ( {$api} , inject) => {
const service = new SomeService($api)
inject('someService', SomeService)
}
So in your case that would be:
export default async function ({ app, route, store, $getAffiliate }) {
const affiliate = $getAffiliate()
console.log(affiliate)
}
Not sure if this is the right way, but it works for me.
I'm using Nuxt JS's inject
function to inject a function into my page for code reusability. I'd like to use the function in another plugin file and can't seem to get the function in there.
Here's my set up:
- plugins/utils/tracking.js
function setCookiePrefix () {
return 'fudge__'
}
function getCookie (app, name) {
try {
const prefix = setCookiePrefix()
const cookie = app.$cookies.get(`${prefix}${name}`)
if (!cookie || cookie == '') {
throw 'cookie not set'
}
return cookie
} catch (err) { }
return null
}
export default function ({ app, store, context }, inject) {
/*
* Get just the affiliate (cpm_id OR affiliate)
* examples: "my_brand", "blah"
*/
inject('getAffiliate', () => {
const affiliate = getCookie(app, 'affiliate')
const brand = getBrand(store)
if (!affiliate) return brand
return affiliate
})
}
And the file I'm trying to utilise the getAffiliate
function in from my tracking.js file:
- plugins/init-brand.js
export default async function ({ app, route, store }) {
const affiliate = app.$getAffiliate()
console.log(affiliate) <-- undefined
}
I've tried:
app.$getAffiliate()
this.$getAffiliate()
<-- this works in a Vue file$getAffiliate()
this.getAffiliate()
What am I missing to access my getAffiliate
function in another plugin file?
I'm using Nuxt JS's inject
function to inject a function into my page for code reusability. I'd like to use the function in another plugin file and can't seem to get the function in there.
Here's my set up:
- plugins/utils/tracking.js
function setCookiePrefix () {
return 'fudge__'
}
function getCookie (app, name) {
try {
const prefix = setCookiePrefix()
const cookie = app.$cookies.get(`${prefix}${name}`)
if (!cookie || cookie == '') {
throw 'cookie not set'
}
return cookie
} catch (err) { }
return null
}
export default function ({ app, store, context }, inject) {
/*
* Get just the affiliate (cpm_id OR affiliate)
* examples: "my_brand", "blah"
*/
inject('getAffiliate', () => {
const affiliate = getCookie(app, 'affiliate')
const brand = getBrand(store)
if (!affiliate) return brand
return affiliate
})
}
And the file I'm trying to utilise the getAffiliate
function in from my tracking.js file:
- plugins/init-brand.js
export default async function ({ app, route, store }) {
const affiliate = app.$getAffiliate()
console.log(affiliate) <-- undefined
}
I've tried:
app.$getAffiliate()
this.$getAffiliate()
<-- this works in a Vue file$getAffiliate()
this.getAffiliate()
What am I missing to access my getAffiliate
function in another plugin file?
-
1
Have you connected the plugins in the configuration in the right order? The plugin with the inject function should be higher in the
plugins: []
innuxt.config.js
to be available in the second plugin file – JanuszO Commented Apr 16, 2022 at 8:37
1 Answer
Reset to default 3I have an api plugin with some axios set up. I can load it inside another plugin file like this:
export default ( {$api} , inject) => {
const service = new SomeService($api)
inject('someService', SomeService)
}
So in your case that would be:
export default async function ({ app, route, store, $getAffiliate }) {
const affiliate = $getAffiliate()
console.log(affiliate)
}
Not sure if this is the right way, but it works for me.
本文标签: javascriptNuxt JS use injected function from plugin in another plugin fileStack Overflow
版权声明:本文标题:javascript - Nuxt JS use injected function from plugin in another plugin file - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572485a2156818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论