Gui*_*hai 26 vue-router vuejs2
据我所知vuex-router-sync仅仅是同步route与 vuex store和开发人员可以访问route如下:
store.state.route.path
store.state.route.params
Run Code Online (Sandbox Code Playgroud)
不过,我也可以处理route由this.$route哪个更简洁.
我什么时候需要在商店中使用路由,以及我需要vuex-router-sync的场景是什么?
cho*_*sia 30
这是我的两分钱.vuex-router-sync如果你不能在项目中弄清楚它的用例,你就不需要导入,但是当你试图route在你vuex的方法中使用object 时你可能想要它(this.$route在vuex的领域中不能很好地工作).
我想在这里举个例子.
假设您要在一个组件中显示消息.您希望Have a nice day, Jack在几乎每个页面中都显示一条消息,但Welcome back, Jack用户浏览首页时应显示的情况除外.
你可以借助它轻松实现它vuex-router-sync.
const Top = {
template: '<div>{{message}}</div>',
computed: {
message() {
return this.$store.getters.getMessage;
}
},
};
const Bar = {
template: '<div>{{message}}</div>',
computed: {
message() {
return this.$store.getters.getMessage;
}
}
};
const routes = [{
path: '/top',
component: Top,
name: 'top'
},
{
path: '/bar',
component: Bar,
name: 'bar'
},
];
const router = new VueRouter({
routes
});
const store = new Vuex.Store({
state: {
username: 'Jack',
phrases: ['Welcome back', 'Have a nice day'],
},
getters: {
getMessage(state) {
return state.route.name === 'top' ?
`${state.phrases[0]}, ${state.username}` :
`${state.phrases[1]}, ${state.username}`;
},
},
});
// sync store and router by using `vuex-router-sync`
sync(store, router);
const app = new Vue({
router,
store,
}).$mount('#app');
// vuex-router-sync source code pasted here because no proper cdn service found
function sync(store, router, options) {
var moduleName = (options || {}).moduleName || 'route'
store.registerModule(moduleName, {
namespaced: true,
state: cloneRoute(router.currentRoute),
mutations: {
'ROUTE_CHANGED': function(state, transition) {
store.state[moduleName] = cloneRoute(transition.to, transition.from)
}
}
})
var isTimeTraveling = false
var currentPath
// sync router on store change
store.watch(
function(state) {
return state[moduleName]
},
function(route) {
if (route.fullPath === currentPath) {
return
}
isTimeTraveling = true
var methodToUse = currentPath == null ?
'replace' :
'push'
currentPath = route.fullPath
router[methodToUse](route)
}, {
sync: true
}
)
// sync store on router navigation
router.afterEach(function(to, from) {
if (isTimeTraveling) {
isTimeTraveling = false
return
}
currentPath = to.fullPath
store.commit(moduleName + '/ROUTE_CHANGED', {
to: to,
from: from
})
})
}
function cloneRoute(to, from) {
var clone = {
name: to.name,
path: to.path,
hash: to.hash,
query: to.query,
params: to.params,
fullPath: to.fullPath,
meta: to.meta
}
if (from) {
clone.from = cloneRoute(from)
}
return Object.freeze(clone)
}Run Code Online (Sandbox Code Playgroud)
.router-link-active {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://unpkg.com/vuex/dist/vuex.js"></script>
<div id="app">
<p>
<router-link to="/top">Go to Top</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
<router-view></router-view>
</div>Run Code Online (Sandbox Code Playgroud)
如您所见,组件vuex与vue-router逻辑分离良好.
对于您不关心当前路由与从vuex的getter返回的值之间的关系的情况,此模式有时可以非常有效.
我在学习 Vue 的时候看到了这个帖子。补充了我对这个问题的一些理解。
Vuex 为 Vue 应用程序定义了一种状态管理模式。我们没有定义组件 props 并在所有地方通过 props 传递共享状态,而是使用集中式存储来组织多个组件共享的状态。对状态突变的限制使状态转换更清晰,更容易推理。
理想情况下,如果提供的商店状态相同,我们应该获取/构建一致(或相同)的视图。但是,由多个组件共享的路由器打破了这一点。如果我们需要推理为什么页面被渲染成这样,如果我们从this.$router属性派生视图,我们需要检查商店状态以及路由器状态。
vuex-router-sync是将路由器状态同步到集中状态存储的助手。现在所有的视图都可以从 state store 构建,我们不需要检查this.$router.
请注意,route状态是不可变的,我们应该通过$router.pushor$router.go调用“改变”它的状态。将 store 上的一些操作定义为:
// import your router definition
import router from './router'
export default new Vuex.Store({
//...
actions: {
//...
// actions to update route asynchronously
routerPush (_, arg) {
router.push(arg)
},
routerGo (_, arg) {
router.go(arg)
}
}
})
Run Code Online (Sandbox Code Playgroud)
这将route更新包装在 store 操作中,我们可以完全摆脱this.$router组件中的依赖关系。