---
title: 從 Vuex 0.6.x 遷移到 1.0
type: guide
order: 703
---
> Vuex 2.0 已經發布了,但是這份指南只涵蓋遷移到 1.0?這是打錯了嗎?此外,似乎 Vuex 1.0 和 2.0 也同時發布。這是怎么回事?我該用哪一個并且哪一個兼容 Vue 2.0呢?
Vuex 1.0 和 2.0 如下:
- 都完全支持 Vue 1.0 和 2.0
- 將在可預見的未來保留支持
然而它們的目標用戶稍微有所不同。
**Vuex 2.0** 從根本上重新設計并且提供簡潔的 API,用于幫助正在開始一個新項目的用戶,或想要用客戶端狀態管理前沿技術的用戶。**此遷移指南不涵蓋 Vuex 2.0 相關內容**,因此如果你想了解更多,請查閱 [Vuex 2.0 文檔](https://vuex.vuejs.org/zh-cn/)。
**Vuex 1.0** 主要是向下兼容,所以升級只需要很小的改動。推薦擁有大量現存代碼庫的用戶,或只想盡可能平滑升級 Vue 2.0 的用戶。這份指南致力促進這一過程,但僅包括遷移說明。完整使用指南請查閱 [Vuex 1.0 文檔](https://github.com/vuejs/vuex/tree/1.0/docs/zh-cn)。
## 帶字符串屬性路徑的 `store.watch` <sup>替換</sup>
`store.watch` 現在只接受函數。因此,下面例子你需要替換:
``` js
store.watch('user.notifications', callback)
```
為:
``` js
store.watch(
// 當返回結果改變...
function (state) {
return state.user.notifications
},
// 執行回調函數
callback
)
```
這幫助你更加完善的控制那些需要監聽的響應式屬性。
```
<div class="upgrade-path">
<h4>升級方法</h4>
<p>在代碼庫運行<a href="https://github.com/vuejs/vue-migration-helper">遷移工具</a>,查找在 <code>store.watch</code> 中使用字符串作為第一個參數的事例。</p>
</div>
```
## Store 的事件觸發器 <sup>移除</sup>
store 實例不再暴露事件觸發器 (event emitter) 接口 (`on`, `off`, `emit`)。如果你之前使用 store 作為全局的 event bus,遷移說明相關內容請查閱[此章節](migration.html#dispatch-和-broadcast-替換)。
為了替換正在使用觀察 store 自身觸發事件的這些接口,(例如:`store.on('mutation', callback)`),我們引入新的方法 `store.subscribe`。在插件中的典型使用方式如下:
``` js
var myPlugin = store => {
store.subscribe(function (mutation, state) {
// Do something...
})
}
```
更多信息請查閱[插件文檔](https://github.com/vuejs/vuex/blob/1.0/docs/en/plugins.md)的示例。
```
<div class="upgrade-path">
<h4>升級方式</h4>
<p>在代碼庫運行<a href="https://github.com/vuejs/vue-migration-helper">遷移工具</a>,查找使用了 <code>store.on</code>, <code>store.off</code>, <code>store.emit</code> 的事例。</p>
</div>
```
## 中間件 <sup>替換</sup>
中間件被替換為插件。插件是接收 store 作為僅有參數的基本函數,能夠監聽 store 中的 mutation 事件:
``` js
const myPlugins = store => {
store.subscribe('mutation', (mutation, state) => {
// Do something...
})
}
```
更多詳情,請查閱 [插件文檔](https://github.com/vuejs/vuex/blob/1.0/docs/en/plugins.md)。
```
<div class="upgrade-path">
<h4>升級方法</h4>
<p>在代碼庫運行<a href="https://github.com/vuejs/vue-migration-helper">遷移工具</a>,查找使用了 <code>middlewares</code> 選項的事例。</p>
</div>
```
- 寫在前面
- 基礎
- 安裝
- 介紹
- Vue實例
- 模板語法
- 計算屬性和偵聽器
- Class 與 Style 綁定
- 條件渲染
- 列表渲染
- 事件處理
- 表單輸入綁定
- 組件基礎
- 深入了解組件
- 組件注冊
- Prop
- 自定義事件
- 插槽
- 動態組件 & 異步組件
- 處理邊界情況
- 過渡 & 動畫
- 進入/離開 & 列表過渡
- 狀態過渡
- 可復用性 & 組合
- 混入
- 自定義指令
- 渲染函數 & JSX
- 插件
- 過濾器
- 工具
- 生產環境部署
- 單文件組件
- 單元測試
- TypeScript 支持
- 規模化
- 路由
- 狀態管理
- 服務端渲染
- 內在
- 深入響應式原理
- 遷移
- 從 Vue 1.x 遷移
- 從 Vue Router 0.7.x 遷移
- 從 Vuex 0.6.x 遷移到 1.0
- 更多
- 對比其他框架
- 加入 Vue.js 社區
- 開發團隊