我爱模板网最近在使用Vue3开发项目时,遇到了一些问题
在2.X版本中创建一个vue 实例是通过 new Vue()来实现的,到了3.X中则是通过使用createApp这个 API返回一个应用实例,并且可以通过链条的方式继续调用其他的方法:
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import qs from "qs";
const app = createApp(App);
app.use(store);
app.use(router);
app.config.globalProperties.$qs = qs;
app.mount("#app");在vue2的语法中使用还是和Vue2一样,直接this.$xx
this.$qs.stringify()
但在composition api和setup语法糖中,由于没有了this,使用起来反而更加麻烦:
// 引入getCurrentInstance
import {getCurrentInstance} from 'vue'
const { proxy } = getCurrentInstance();
// 取出定义的qs
const {$qs}=proxy
// 调用
console.log($qs.state.token)