我可以在没有服务器端实现(server.js)的情况下使用 Redux,只在客户端呈现吗?挑战是在不更改我当前的服务器设置(Apache + PHP)的情况下重写 Web 应用程序的一部分。
问问题
45 次
1 回答
2
server.js
Redux 示例中只是为了方便开发而运行 Webpack 开发服务器。它与 Redux 无关,就像 Webpack,或 Grunt,或 Gulp,或 npm 与 Redux 无关。这些是完全不相关的技术。
您可能会发现该counter-vanilla
示例很有用。它展示了如何在没有任何构建系统的情况下使用 Redux:
<!DOCTYPE html>
<html>
<head>
<title>Redux basic example</title>
<script src="https://npmcdn.com/redux@latest/dist/redux.min.js"></script>
</head>
<body>
<div>
<p>
Clicked: <span id="value">0</span> times
<button id="increment">+</button>
<button id="decrement">-</button>
<button id="incrementIfOdd">Increment if odd</button>
<button id="incrementAsync">Increment async</button>
</p>
</div>
<script>
function counter(state, action) {
if (typeof state === 'undefined') {
return 0
}
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
var store = Redux.createStore(counter)
var valueEl = document.getElementById('value')
function render() {
valueEl.innerHTML = store.getState().toString()
}
render()
store.subscribe(render)
document.getElementById('increment')
.addEventListener('click', function () {
store.dispatch({ type: 'INCREMENT' })
})
document.getElementById('decrement')
.addEventListener('click', function () {
store.dispatch({ type: 'DECREMENT' })
})
document.getElementById('incrementIfOdd')
.addEventListener('click', function () {
if (store.getState() % 2 !== 0) {
store.dispatch({ type: 'INCREMENT' })
}
})
document.getElementById('incrementAsync')
.addEventListener('click', function () {
setTimeout(function () {
store.dispatch({ type: 'INCREMENT' })
}, 1000)
})
</script>
</body>
</html>
于 2016-04-15T22:44:13.730 回答