UNPKG

3.87 kBMarkdownView Raw
1<h1 align="center">Revue</h1>
2
3<p align="center">
4 <a href="https://www.npmjs.com/package/revue">
5 <img src="https://camo.githubusercontent.com/b145895dcb12693255d3b4b371446ea6b73fa357/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f72657675652e737667" alt="NPM version" style="max-width:100%;">
6 </a>
7 <a href="https://www.npmjs.com/package/revue">
8 <img src="https://camo.githubusercontent.com/49a99ffd8da7a0793e1d648f859792e9b1db45fa/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f72657675652e737667" alt="NPM download" style="max-width:100%;"></a>
9 <a href="https://semaphoreci.com/egoist/revue">
10 <img src="https://camo.githubusercontent.com/76d2c0872d04fb30683774e965ed8c717959ef77/68747470733a2f2f73656d6170686f726563692e636f6d2f6170692f76312f70726f6a656374732f39383639643839662d313632312d343831332d386331362d3663663065373465623862622f3633333038322f62616467652e737667" alt="Build Status" style="max-width:100%;">
11 </a>
12</p>
13
14<p align="center">
15 <a href="https://github.com/egoist/vuepack">
16 <img src="https://cloud.githubusercontent.com/assets/8784712/11776407/cb9d0838-a281-11e5-8d75-c6b2a7c9978f.png" alt="vuepack">
17 </a>
18</p>
19
20## Usage
21
22**New:** [Time to use Redux-devtools in non-React apps!](https://github.com/egoist/redux-devtools-script)
23
24Obviously it works with Redux, install via NPM: `npm i -D redux revue`
25
26You can also hot-link the CDN version: https://npmcdn.com/revue/revue.js, `Revue` is exposed to `window` object.
27
28```javascript
29// App.js
30import Revue from 'revue'
31import store from './store'
32Vue.use(Revue, {
33 store
34})
35
36// store.js
37// just put some reducers in `./reducers` like
38// what you do in pure Redux
39// and combine them in `./reducers/index.js`
40import { createStore } from 'redux'
41import reducer from './reducers/index'
42export default createStore(reducer)
43
44// component.js
45// some component using Revue
46new Vue({
47 el: '#app',
48 data () {
49 return {
50 counter: this.$revue.getState().counter
51 }
52 },
53 ready () {
54 // subscribe state changes
55 this.$subscribe('counter')
56 // if your name the 'counter' to 'temp_counter' in data()
57 // you can use this.$subscribe('counter as temp_counter')
58 // if you want to subscribe a deep property
59 // this.$subscribe('top.middle.counter as counter')
60 // or even this.$subscribe('something.in.reduxStore.counter as instance.somewhere.counter')
61 },
62 methods: {
63 handleClickCounter () {
64 // dispatch events
65 this.$revue.dispatch({type: 'INCREMENT'})
66 }
67 }
68})
69```
70
71[**More detailed usages**](/src)
72
73## Hot-reload reducers
74
75Just change your `store.js` like this:
76
77Before:
78
79```javascript
80import { createStore } from 'redux'
81import rootReducer from './reducers'
82
83export default createStore(rootReducer)
84```
85
86After:
87
88```javascript
89import { createStore } from 'redux'
90import rootReducer from './reducers'
91
92function configureStore() {
93 const store = createStore(rootReducer)
94 if (module.hot) {
95 module.hot.accept('./reducers', () => {
96 const nextRootReducer = require('./reducers').default
97 store.replaceReducer(nextRootReducer)
98 })
99 }
100 return store
101}
102
103export default configureStore()
104```
105
106## FAQ
107
108**Do I have to use `this.$subscribe`? It's so verbose.**
109
110No, not always if you don't care about mutating states in reducers. And also because Vue states are mutable and observable, it's ok for you to modify data directly like `state.foo = 'bar'`, then it becomes so similar to the Vue Flux implementation [Vuex](https://github.com/vuejs/vuex), it allows you to mutate data. However what the best part of Redux is states are immutable, which means you can't make direct operations on states so that you have less chance to make mistakes.
111
112`this.$subscribe` is only needed if you don't mutate states directly. And you're recommended to do so.
113
114## License
115
116MIT &copy; [EGOIST](https://github.com/egoist)