gpt4 book ai didi

javascript - 如何从 nuxt.js 商店访问插件?

转载 作者:行者123 更新时间:2023-12-05 00:34:53 25 4
gpt4 key购买 nike

我有这个 gtag(分析插件),我可以在我的组件上访问它,但不能在我的商店中访问。
我会很感激任何意见。谢谢
插件/vue-gtag.js

import Vue from "vue"
import VueGtag from "vue-gtag"

export default ({ app }, inject) => {
Vue.use(VueGtag, {
config: {
id: process.env.ga_stream_id
}
})
}

商店/gaUserProperty.js
import Vue from "vue"
import { User } from "~/models/user/User"

export const states = () => ({})

const getterObjects = {}
const mutationObjects = {}
Object.keys(states).forEach(key => {
getterObjects[key] = state => state[key]
mutationObjects[key] = (state, value) => (state[key] = value)
})

export const state = () => states

export const getters = { ...getterObjects }

export const mutations = { ...mutationObjects }

export const actions = {
async sendUserProperties({ dispatch, commit }) {
let res = await this.$UserApi.getUser()
if (!(res instanceof User)) {
} else {
// I can access this on my components and pages but for some reason not here....
console.log(this.$gtag)
}
}
}

最佳答案

为了正确导入它,我将从 main.(ts|js) 导出实例(或其任何内部结构) :

const Instance = new Vue({...whatever});

// only export what you need in other parts of the app
export const { $gtag, $store, $t, $http } = Instance;

// or export the entire Instance
export default Instance;
现在您可以将其导入您的商店:
import Instance from '@/main';
// or:
import { $gtag } from '@/main';

// use Instance.$gtag or $gtag, depending on what you imported.

正如其他答案所提到的,在当前的 Vuex 版本中,Vue 实例在 this._vm 下可用。店内。不过,我不会依赖它,因为它不是暴露的 Vuex API 的一部分,也没有在任何地方记录。换句话说,Vuex 开发人员不保证它在 future 的版本中仍然存在。
更具体地说,Vue 的 promise 是所有 v2代码将在 v3 中工作.但前提是您使用公开的 API。
这里的讨论甚至不是关于它是否会被删除(很可能不会)。对我来说,这更像是一个原则问题:如果它以 _ 开头它没有记录在案,它转化为作者的信息:“我们保留随时更改此内容的权利,恕不另行通知!”

关于javascript - 如何从 nuxt.js 商店访问插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63441665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com