vue3利用install/app.use()挂载公共方法组件

1.在src下创建文件publicTools.js

import store from './store/index'
import router from './/router/index'
import $Fn from './utils/fn' // 方法所在的js
const install = (app) => {
  app.use(store)
  app.use(router)
  // 挂在公共方法
  Object.defineProperties(app.config.globalProperties, {
    $Fn: {
      get () {
        return $Fn
      }
    }
  })
}
export default install

2.在 main.js 文件中注册使用

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

// import $Fn from './utils/fn'
import publicTools from './publicTools'

// 创建appvue
const app = createApp(App)
// app.config.globalProperties.$Fn = $Fn
app.use(store)
app.use(router)
app.use(publicTools)
app.mount('#app')

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