UNPKG

3.83 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://img.shields.io/travis/egoist/revue/master.svg" 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.$store.state.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 // you can only $subscribe once, if you want to subscribe multi states at the same time, do this:
62 /*
63 this.$subscribe(
64 'foo',
65 'bar'
66 )
67 */
68 },
69 methods: {
70 handleClickCounter () {
71 // dispatch events
72 this.$store.dispatch({type: 'INCREMENT'})
73 }
74 }
75})
76```
77
78[**More detailed usages**](/src)
79
80## Hot-reload reducers
81
82Just change your `store.js` like this:
83
84Before:
85
86```javascript
87import { createStore } from 'redux'
88import rootReducer from './reducers'
89
90export default createStore(rootReducer)
91```
92
93After:
94
95```javascript
96import { createStore } from 'redux'
97import rootReducer from './reducers'
98
99function configureStore() {
100 const store = createStore(rootReducer)
101 if (module.hot) {
102 module.hot.accept('./reducers', () => {
103 const nextRootReducer = require('./reducers').default
104 store.replaceReducer(nextRootReducer)
105 })
106 }
107 return store
108}
109
110export default configureStore()
111```
112
113## FAQ
114
115**Do I have to use `this.$subscribe`? It's so verbose.**
116
117No, 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.
118
119`this.$subscribe` is only needed if you don't mutate states directly. And you're recommended to do so.
120
121## License
122
123MIT &copy; [EGOIST](https://github.com/egoist)