1 Star 0 Fork 0

Vue.js / vuex-router-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Vuex Router Sync

npm ci status coverage license

把 Vue Router 当前的 $route 同步为 Vuex 状态的一部分。

English

用法

# 最新版本需要配合 vue-router 2.0 及以上的版本使用
npm install vuex-router-sync
# 用于版本低于 2.0 的 vue-router
npm install vuex-router-sync@2
import { sync } from 'vuex-router-sync'
import store from './vuex/store' // vuex store 实例
import router from './router' // vue-router 实例

const unsync = sync(store, router) // 返回值是 unsync 回调方法

// 在这里写你的代码

// 在 Vue 应用销毁时 (比如在仅部分场景使用 Vue 的应用中跳出该场景且希望销毁 Vue 的组件/资源时)
unsync() // 取消 store 和 router 中间的同步

你可以有选择地设定一个自定义的 vuex 模块名:

sync(store, router, { moduleName: 'RouteModule' } )

工作原理

  • 该库在 store 上增加了一个名为 route 的模块,用于表示当前路由的状态。

    store.state.route.path   // current path (字符串类型)
    store.state.route.params // current params (对象类型)
    store.state.route.query  // current query (对象类型)
  • 当被导航到一个新路由时,store 的状态会被更新。

  • store.state.route 是不可变更的,因为该值取自 URL,是真实的来源。你不应该通过修改该值去触发浏览器的导航行为。取而代之的是调用 $router.push() 或者 $router.go()。另外,你可以通过 $router.push({ query: {...}}) 来更新当前路径的查询字符串。

The MIT License (MIT) Copyright (c) 2016-present Evan You Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Effortlessly keep vue-router and vuex store in sync. 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/vuejs/vuex-router-sync.git
git@gitee.com:vuejs/vuex-router-sync.git
vuejs
vuex-router-sync
vuex-router-sync
master

搜索帮助