gpt4 book ai didi

vuejs2 - vuex-router-sync 有什么用?

转载 作者:行者123 更新时间:2023-12-03 10:26:16 25 4
gpt4 key购买 nike

据我所知vuex-router-sync仅用于同步 routevuex store并且开发者可以访问route如下:

store.state.route.path
store.state.route.params

不过,我也可以处理 route来自 this.$route哪个更简洁。

什么时候需要使用store中的路由,需要vuex-router-sync的场景是什么?

最佳答案

这是我的两分钱。您不需要导入 vuex-router-sync如果您无法在您的项目中找出它的用例,但是当您尝试使用 route 时可能需要它。您的 vuex 中的对象的方法( 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)
}
.router-link-active {
color: red;
}
<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>

fiddle here

如您所见,组件与 vuex 很好地解耦。和 vue-router的逻辑。
对于您不关心当前路由与 vuex 的 getter 返回值之间的关系的情况,这种模式有时非常有效。

关于vuejs2 - vuex-router-sync 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44171210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com