广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue3如何自定义js文件(插件或配置)
  • 954
分享到

vue3如何自定义js文件(插件或配置)

vue3自定义js文件vue3插件vue3配置 2022-11-13 14:11:25 954人浏览 独家记忆
摘要

目录vue3自定义js文件举例腾讯防水墙js调用文件Vue加载自定义的js文件效果图遇见的问题使用vue3自定义js文件 在vue3中自定义的js文件,如果需要设置全局this.xx

vue3自定义js文件

在vue3中自定义的js文件,如果需要设置全局this.xxx调用方式的话,需要给方法、变量、常量export出去,调用install()方法

插件的功能范围没有严格的限制——一般有下面几种:

添加全局方法或者 property。如:vue-custom-element

添加全局资源:指令/过滤器/过渡等。如:vue-touch

通过全局混入来添加一些组件选项。如:vue-router

添加全局实例方法,通过把它们添加到 config.globalProperties 上实现。

一个库,提供自己的 api,同时提供上面提到的一个或多个功能。如 vue-router

export default {
  install: (app) => {
  }
 }

举例腾讯防水墙js调用文件

v2

// TencentCaptcha.js
import Vue from 'vue';
const appId = '*********';
Vue.prototype.$txCaptcha = (cb) => {
  const t = new window.TencentCaptcha(appId, (rsp) => {
    t.destroy();
    cb(rsp);
  }, {});
  t.show();
};
// main.js
import './config/TencentCaptcha';

使用

export default {
// ...
methods:{
    getCode () {
        this.$txCaptcha((res) => {
            this.txResult = res;
        });
    }
}
}

v3

// TencentCaptcha.js
const appId = '*********';
export default {
  install: (app) => {
    const Vue = app;
    Vue.config.globalProperties.$txCaptcha = (cb) => {
      const t = new window.TencentCaptcha(appId, (rsp) => {
        t.destroy();
        cb(rsp);
      }, {});
      t.show();
    };
  },
};
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import txCaptcha from './config/TencentCaptcha';
createApp(App).use(txCaptcha)

使用

<script setup lang="ts">
import {getCurrentInstance} from 'vue'
getCurrentInstance().appContext.config.globalProperties.$txCaptcha((res) => {
    this.txResult = res;
});
</script>

vue加载自定义的js文件

在做项目中需要自定义弹出框。就自己写了一个。

效果图

这里写图片描述

遇见的问题

怎么加载自定义的js文件

vue-插件这必须要看。然后就是自己写了。

export default{
    install(Vue){
        var tpl;
        // 弹出框
        Vue.prototype.showAlter = (title,msg) =>{
            var alterTpl = Vue.extend({     // 1、创建构造器,定义好提示信息的模板
                    template: '<div id="bg">'
                         + '<div class="jfalter">'
                         + '<div class="jfalter-title" id="title">'+ title +'</div>'
                         + '<div class="jfalter-msg" id="message">'+ msg +'</div>'
                         + '<div class="jfalter-btn" id="sureBtn" v-on:click="hidealter">确定</div>'
                         + '</div></div>'
            });
            tpl = new alterTpl().$mount().$el;  // 2、创建实例,挂载到文档以后的地方
            document.body.appendChild(tpl);  
        }
        Vue.mixin({
          methods: {
            hideAlter: function () {
              document.body.removeChild(tpl);
            }
          }
        })
    }
}

使用

import jFAltre from '../../assets/jfAletr.js';
import Vue from 'vue';
Vue.use(jFAltre);
this.showAlter('提示','服务器请求失败');

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: vue3如何自定义js文件(插件或配置)

本文链接: https://www.lsjlt.com/news/172170.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作