在第三方脚本(NUXT ^2.15.3)中使用商店和路线

发布于 2025-02-13 00:55:12 字数 1412 浏览 0 评论 0原文

NUXT上的项目 ^2.15.3。我有一个可以在数据库中执行异步查询的类,我需要导入商店和路由,以便可以在内部使用它们,现在当我在构造函数中初始化类时,我为自己分配了一个路由器和一个商店,但是问题是,当在不同页面上提出请求时,路由器参数不动态,它不会改变,是的,我可以从asyncdata转移存储和路由器,但是这是独立的用途没有商店和路由器的不断转移。

requestMongo.js

export default class {
constructor({ store, $axios, i18n, $config, route }) {
    this.api = $axios.create({
        baseURL: `${$config.baseURL}/${$config.mongoURL}`,
        method: 'post', // default
        headers: {
            common: {
                Accept: "application/json",
            }
        }
    });
    this.store = store;
    this.i18n = i18n;
    this.route = route; - the router is static and does not show changes when you navigate to other pages
}

函数

export default async function ({ store, $axios , i18n , route, $config }, inject) {
const requestMongo = new RequestMongo({ store,$axios,i18n,route,$config });
inject('requestMongo', requestMongo);}

.js nuxt.config.js

  plugins: [
{ src: '@/plugins/functions.js' },]

我尝试导入商店,但

import store from '../../store'

但是

import {store} from '../../store'

存储是nes undefined

是否可以在同类产品中调用实际的商店和路由器而无需求助于

 async asyncData({$requestMongo,route,$store}) {
    return await $requestMongo.getSingleSlugPage({route,$store})
},

您的时间

project on nuxt ^2.15.3. I have a class that performs asynchronous queries to the database, I need to import the store and route so that I can use them inside, now when I initialize the class in the constructor, I assign myself a router and a store, but the problem is that when making requests on different pages, the router parameter not dynamic, it does not change ,yes i can transfer store and router from asyncData ,but it is the independent use that is of interest without the constant transfer of the store and the router.

requestMongo.js

export default class {
constructor({ store, $axios, i18n, $config, route }) {
    this.api = $axios.create({
        baseURL: `${$config.baseURL}/${$config.mongoURL}`,
        method: 'post', // default
        headers: {
            common: {
                Accept: "application/json",
            }
        }
    });
    this.store = store;
    this.i18n = i18n;
    this.route = route; - the router is static and does not show changes when you navigate to other pages
}

function.js

export default async function ({ store, $axios , i18n , route, $config }, inject) {
const requestMongo = new RequestMongo({ store,$axios,i18n,route,$config });
inject('requestMongo', requestMongo);}

nuxt.config.js

  plugins: [
{ src: '@/plugins/functions.js' },]

I tried to import the store but

import store from '../../store'

or

import {store} from '../../store'

But store is undefined

is it possible to call the actual store and router in the class without resorting to

 async asyncData({$requestMongo,route,$store}) {
    return await $requestMongo.getSingleSlugPage({route,$store})
},

thanks for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文