共计 633 个字符,预计需要花费 2 分钟才能阅读完成。
Vue3.x 用 Composition API 中是没有 this 的,所以不能间接应用 this 调用 globalProperties 全局变量
Composition API 应用办法
// main.js
import {createApp} from 'vue'
import App from './App.vue'
import storage from './plugins/storage'
const app = createApp(App);
app.config.globalProperties.$storage = storage
app.provide('$storage', storage)
app.mount('#app')
// Home.vue
import {inject} from "vue"
const storage = inject('$storage')
storage.setItme('name','zhangsan')
Vue2.x 的应用办法
// main.js
import {createApp} from 'vue'
import App from './App.vue'
import storage from './plugins/storage'
const app = createApp(App);
app.config.globalProperties.$storage = storage
app.mount('#app')
// Home.vue
this.$storage.setItme('name','zhangsan')
正文完