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