如何在拆分 (vuex) getters.js 文件中访问 $route?
How to access $route within a splitted (vuex) getters.js file?
我将 vuex 用作拆分 (getters.js、mutations.js、actions.js、state.js) 文件,我想在拆分中使用 $route 插件变量getters.js 文件。实际上,我之前将它们合而为一,但我切换到 quasar,而 quasar 建议以某种方式在拆分文件中使用 vuex 模块。好吧,这不是建议,但那里有一个例子。我知道我可以用旧的方式使用它,但我想试试这个,由于我缺乏知识,我不确定。所以就像:
└── store
└─ routes
├── state.js
├── actions.js
├── mutations.js
├── getters.js
└── index.js
在index.js中,它们都像这样绑定:
import state from './state'
import * as getters from './getters'
import * as mutations from './mutations'
import * as actions from './actions'
export default {
namespaced: true,
getters,
mutations,
actions,
state
}
好吧,在 getters.js 文件中,我导出了我以前使用过的函数。但是我需要访问其中一个中的 $route 插件。
export const current_route = state => {
return this.$route;
}
顺便说一句,我也知道我可以在任何需要的地方直接使用 this.$route(不使用 vuex getter),但我只想知道如何访问这些变量(或即使有可能)在这些拆分文件中。
下面是vue-router
的源代码:
Vue.mixin({
beforeCreate () {
if (isDef(this.$options.router)) {
/* something else */
Vue.util.defineReactive(this, '_route', this._router.history.current)
} else {
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
}
/* something else */
}
})
Object.defineProperty(Vue.prototype, '$route', {
get () { return this._routerRoot._route }
})
如您所见,$route
是$router.history.current
的别名。
所以可以直接导入router
:
import router from 'path/to/your/router'
export const current_route = state => {
return router.history.current
}
我将 vuex 用作拆分 (getters.js、mutations.js、actions.js、state.js) 文件,我想在拆分中使用 $route 插件变量getters.js 文件。实际上,我之前将它们合而为一,但我切换到 quasar,而 quasar 建议以某种方式在拆分文件中使用 vuex 模块。好吧,这不是建议,但那里有一个例子。我知道我可以用旧的方式使用它,但我想试试这个,由于我缺乏知识,我不确定。所以就像:
└── store
└─ routes
├── state.js
├── actions.js
├── mutations.js
├── getters.js
└── index.js
在index.js中,它们都像这样绑定:
import state from './state'
import * as getters from './getters'
import * as mutations from './mutations'
import * as actions from './actions'
export default {
namespaced: true,
getters,
mutations,
actions,
state
}
好吧,在 getters.js 文件中,我导出了我以前使用过的函数。但是我需要访问其中一个中的 $route 插件。
export const current_route = state => {
return this.$route;
}
顺便说一句,我也知道我可以在任何需要的地方直接使用 this.$route(不使用 vuex getter),但我只想知道如何访问这些变量(或即使有可能)在这些拆分文件中。
下面是vue-router
的源代码:
Vue.mixin({
beforeCreate () {
if (isDef(this.$options.router)) {
/* something else */
Vue.util.defineReactive(this, '_route', this._router.history.current)
} else {
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
}
/* something else */
}
})
Object.defineProperty(Vue.prototype, '$route', {
get () { return this._routerRoot._route }
})
如您所见,$route
是$router.history.current
的别名。
所以可以直接导入router
:
import router from 'path/to/your/router'
export const current_route = state => {
return router.history.current
}