vue3에서 공개 메소드를 마운트하는 방법. Vue3는 public axios를 마운트하고 public regular를 마운트합니다.

vue3에서 공개 axios를 마운트하는 방법,     axios 설치 cnpm install axios --save

// -g 전역으로 설치
// --save/ 속기 -S 프로덕션에서 로컬로 설치
// --save-dev/ 속기로 -D 개발에서 로컬로 설치

 다음과 같이 일반 페이지  를 작성하고 src 아래에 utils > regexp.js 파일을 생성하여 일반 페이지에 넣기  (간단한 전화번호 인증)

export const phone = (res) => {
    let reg = /^1[3,5,6,7,8,9][0-9]{9}$/
    return reg.test(res)
}

main.js 파일 은    주로 app.config.globalProperties 를 통해 공개 메소드를 마운트합니다. 

import {
    createApp
} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'


import axios from 'axios' // 引入axios
import { phone  } from './utils/regexp' //  这个是正则
    
const app = createApp(App)

app.config.globalProperties.http = axios // 这个是 axios
app.config.globalProperties.regexp = { // 正则 在哪个页面都可以使用
    phone
}

app.use(store).use(router).mount('#app')

페이지에서 사용? 주로  프록시 를 통해 가져오기

<template>
  <div class="">
  
  </div>
</template>

<script>
import { getCurrentInstance, reactive, toRefs } from "vue";
export default {
  setup() {

    const instance = getCurrentInstance(); //  调用getCurrentInstance方法
    // 第一种方式 获取全局的公共方法 请求
    // const http = instance.appContext.config.globalProperties.http;
    // 第二种通过 proxy 获取全局 的方法
    const http = instance.proxy.http;
    // 获取全局封装好的 正则方法
    const { phone } = instance.proxy.regexp;

    //  请求
    http
      .get("https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata")
      .then((res) => {
        console.log(res);
      });

    console.log(phone(1222222036)); // 这个使用正则验证手机号的正则
    return { };
  },
};
</script>

<style lang="scss" scoped></style>

  젊은 게으름뱅이, 늙은 거지

추천

출처blog.csdn.net/qq_54753561/article/details/122242601