首页 > 解决方案 > 如何在外部脚本中使用 nuxt 模块?

问题描述

我正在使用nuxt. 我创建了一个名为 services 的目录,其中包含一些脚本。我想在脚本中使用@nuxt/device模块。模块的用法是这样的this.$device.isDesktop。但是在外部脚本中,我无法访问nuxtthis.

我的项目结构是这样的:

在此处输入图像描述

我想在search_report.js文件中使用这个包。的内容search_report.js是这样的:

import Api from '~/services/api';

export default class SearchReport{

    static instance = undefined;

    search_query_id = undefined;

    static get_instance(){
        if(SearchReport.instance === undefined){
            SearchReport.instance = new SearchReport();
        }
        return SearchReport.instance;
    }

    log_search_query(query, success_callback, error_callback){
        let self = this;
        Api.send_ajax_get('http://example.com/add_search_query/' + query + '/', data, function(result) {
                self.search_query_id = result.data.search_query_id;
                if(success_callback){
                    success_callback();
                }
            },error_callback);
    }
}

我想在log_search_query方法中使用这个包将设备类型发送到服务器。

如何nuxt-module在外部脚本中使用 a?

标签: javascriptvue.jssingle-page-applicationnuxt.js

解决方案


推荐阅读