diff --git a/packages/@vuepress/core/lib/app/Store.js b/packages/@vuepress/core/lib/app/Store.js deleted file mode 100644 index cf3e5b60b9..0000000000 --- a/packages/@vuepress/core/lib/app/Store.js +++ /dev/null @@ -1,19 +0,0 @@ -import Vue from 'vue' - -export default class Store { - constructor () { - this.store = new Vue({ - data: { - ob: {} - } - }) - } - - get (key) { - return this.store.ob[key] - } - - set (key, value) { - Vue.set(this.store.ob, key, value) - } -} diff --git a/packages/@vuepress/core/lib/app/app.js b/packages/@vuepress/core/lib/app/app.js index 272ca04262..2e7b1c864b 100644 --- a/packages/@vuepress/core/lib/app/app.js +++ b/packages/@vuepress/core/lib/app/app.js @@ -7,7 +7,7 @@ import { siteData } from '@internal/siteData' import appEnhancers from '@internal/app-enhancers' import globalUIComponents from '@internal/global-ui' import ClientComputedMixin from '@transform/ClientComputedMixin' -import Store from './Store' +import Store from './plugins/Store' // built-in components import Content from './components/Content' @@ -30,9 +30,8 @@ if (module.hot) { Vue.config.productionTip = false -Vue.$store = new Store() - Vue.use(Router) +Vue.use(Store, '$vuepress') // mixin for exposing $site and $page Vue.mixin(dataMixin(ClientComputedMixin, siteData)) // component for rendering markdown content and setting title etc. @@ -62,7 +61,7 @@ export function createApp (isServer) { if (saved) { return saved } else if (to.hash) { - if (Vue.$store.get('disableScrollBehavior')) { + if (Vue.$vuepress.$get('disableScrollBehavior')) { return false } return { diff --git a/packages/@vuepress/core/lib/app/dataMixin.js b/packages/@vuepress/core/lib/app/dataMixin.js index 1f0b2dfa18..7e0acc5a45 100644 --- a/packages/@vuepress/core/lib/app/dataMixin.js +++ b/packages/@vuepress/core/lib/app/dataMixin.js @@ -4,16 +4,16 @@ import Vue from 'vue' export default function dataMixin (I18n, siteData) { prepare(siteData) - Vue.$store.set('siteData', siteData) + Vue.$vuepress.$set('siteData', siteData) if (module.hot) { module.hot.accept(VUEPRESS_TEMP_PATH + '/internal/siteData.js', () => { prepare(siteData) - Vue.$store.set('siteData', siteData) + Vue.$vuepress.$set('siteData', siteData) }) } - const I18nConstructor = I18n(Vue.$store.get('siteData')) + const I18nConstructor = I18n(Vue.$vuepress.$get('siteData')) const i18n = new I18nConstructor() const descriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(i18n)) const computed = {} diff --git a/packages/@vuepress/core/lib/app/plugins/Store.js b/packages/@vuepress/core/lib/app/plugins/Store.js new file mode 100644 index 0000000000..ba84d258ab --- /dev/null +++ b/packages/@vuepress/core/lib/app/plugins/Store.js @@ -0,0 +1,35 @@ +import Vue from 'vue' + +class Store { + constructor () { + this.store = new Vue({ + data: { + state: {} + } + }) + } + + $get (key) { + return this.store.state[key] + } + + $set (key, value) { + Vue.set(this.store.state, key, value) + } + + $emit (...args) { + this.store.$emit(...args) + } + + $on (...args) { + this.store.$on(...args) + } +} + +export default { + install (Vue, options = '$store') { + const store = new Store() + Vue[options] = store + Vue.prototype[options] = store + } +}