1、新增 createApp

入口文件main.js的挂载方式

  1. vue2.x
  2. import Vue from 'vue'
  3. import App from './App.vue'
  4. new Vue({
  5. router,
  6. store,
  7. render: h => h(App)
  8. }).$mount('#app')
  1. Vue.use();
  2. Vue.component();
  3. Vue.prototype.$http = axios;

Vue2.x是的全局api,是直接修改了Vue对象的一系列属性。

Vue2.x全局API遇到的问题

  1. - 单元测试中,全局配置非常容易污染全局环境
  2. - 在不同的apps中,需要共享一份有不同配置的Vue对象,也变得非常困难
  1. vue3.x
  2. import { createApp } from 'vue'
  3. import App from './App.vue'
  4. import router from './router'
  5. import store from './store'
  6. const app = createApp(App)
  7. app.use(store).use(router).mount('#app')

2、修改

2.x全局API 3.x实例API(app)
Vue.config app.config
Vue.config.productionTip
Vue.config.ignoredElements app.config.isCustomElement
Vue.component app.component
Vue.directive app.directive
Vue.mixin app.mixin
Vue.use app.use
Vue.prototype app.config.globalProperties
  1. app = createApp();
  1. import Axios from "axios";
  2. const app=createApp(App)
  3. app.config.globalProperties={
  4. $axios:axios,
  5. name:"lisi"
  6. }
  7. app.component('child-component', {
  8. mounted() {
  9. console.log(this.name); // 'lisi'
  10. }
  11. });