Vue3.xComposition 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')
Logo

前往低代码交流专区

更多推荐