// main.js
import { createApp } from 'vue';
import App from './App.vue';
const app = createApp(App);
// Injecting a property globally
app.config.globalProperties.$myInjectedProperty = 'Hello from injected property';
// Injecting a method globally
app.config.globalProperties.$myInjectedMethod = () => {
console.log('This is an injected method');
};
app.mount('#app');
// In any component
// You can use the injected properties or methods like this:
// <script>
// export default {
// mounted() {
// console.log(this.$myInjectedProperty); // Output: Hello from injected property
// this.$myInjectedMethod(); // Output: This is an injected method
// }
// }
// </script>
全局属性注入:
app.config.globalProperties
可以向所有组件中注入自定义的属性或方法。$myInjectedProperty
和一个方法 $myInjectedMethod
。在组件中使用:
this.$myInjectedProperty
或 this.$myInjectedMethod
访问。注意事项:
app.config.globalProperties
实现的,而在 Vue 2 中则是通过 Vue.prototype
实现的。上一篇:vue vconsole
下一篇:vue3获取dom
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站