vue3 挂载全局对象,如何在组件内使用?

1:vue2挂载方法:

Vue.prototype.$http = http
//在对应的组件中使用
this.$http

2:vue3挂载方法:

// 全局挂载
const app = createApp(App)
app.config.globalProperties.$Methods = Methods;
//挂载全局对象
app.config.globalProperties.$CONFIG = config;
app.config.globalProperties.$TOOL = tool;
//在组件内使用
import { ref, reactive, onMounted,getCurrentInstance } from "vue";

//vue3是组合式API,所以要引入 getCurrentInstance 
const { proxy } = getCurrentInstance();
const $TOOL = proxy.$TOOL;
const $Methods = proxy.$Methods;


//时间格式化
inputValue.value = $TOOL.dateFormat(inputValue.value, 'yyyy-MM-dd hh:mm:ss')
//数组去重
 const jsonarrreduce = reactive([
      { id: "1", name: "李白" },
      { id: "2", name: "杜甫" },
      { id: "3", name: "白居易" },
      { id: "4", name: "项羽" },
      { id: "5", name: "小米" },
      { id: "1", name: "红米" },
      { id: "1", name: "诺基亚" },
      { id: "2", name: "真我" },
    ]);
    onMounted(() => {
      console.log($Methods.JsonArrReduce(jsonarrreduce, "id"));
    });

你可能感兴趣的:(vue3,javascript,前端,vue.js)