반응형
플러그인을 통해 전역적으로 필요한 기능을 추가할 수 있다.
먼저 코드를 작성한다.
x// MyPlugin.jsimport Vue from 'vue'export const MyPlugin = { install (Vue) { Vue.prototype.$myMethod = function() { console.log("method1"); } }}Vue.use(MyPlugin)프로젝트 생성후 main.js의 vue 인스턴스에 플러그인을 추가한다.
xxxxxxxxxximport Vue from 'vue'import App from './App.vue'import MyPlugin from "@/plugins/MyPlugin";Vue.config.productionTip = falsenew Vue({ MyPlugin, render: h => h(App),}).$mount('#app')이 후 컴포넌트에서는 아래와 같이 메서드를 호출할 수 있다.
xxxxxxxxxxthis.$myMethod();참고
반응형