다음과 같이 정의 된 모델에서 내 작업을 중앙 집중화하고 싶습니다.
class Foobar {
doit() {
console.log('I am doing it');
}
}
let foobar = new Foobar()
const app = new Vue({
store,
router,
foobar,
...
})
나중에 하위 구성 요소에서 모델에 대한 작업을 트리거하고 싶습니다.
<template>
<b-button @click="doit">Do It</b-button>
</template>
<script>
export default {
methods: {
doit() {
this.$foobar.doit(); // <----
}
}
}
</script>
저 $foobar
아래로는 접근 할 수없는 것 같습니다 .
Vue에서 외부 싱글 톤에서 작업을 중앙 집중화하는 적절한 방법은 무엇입니까?
이런 식으로 할 수는 있지만 데이터가 얼마나 잘 유지 될지 모르겠습니다.
Vue.prototype.$foobar = foobar;
Vuex를 사용하는 것이 더 좋을 수 있으므로 앱을 통해 유지됩니다.
매장 내부 :
const state = {
foobar: Foobar
}
const mutations = {
initialize (state) {
state.foobar = new Foobar();
}
}
main / app.js 내부 :
store.commit('initialize')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다