+
95
-

回答

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。Vuex 也集成到 Vue 的官方调试工具 devtools extension,提供了诸如零配置的 time-travel 调试、状态快照导入导出等高级调试功能。


看一个示例,通过vuex实现组件之间的值传递效果

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">


<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
:root {
font-size: 16px;
}

* {
box-sizing: border-box;
}

html, body {
height: 100%;
margin: 0;
padding: 0;
}

#app {
min-height: 100%;
display: flex;
flex-direction: column;
}
#app .parent, #app .split {
display: flex;
height: 50vh;
}
#app .parent {
border-bottom: 1px solid #ddd;
padding: 1rem;
align-items: center;
justify-content: center;
text-align: center;
flex-direction: column;
}
#app .parent small {
color: #999;
}
#app .parent .editor {
display: flex;
align-items: center;
margin-top: 1.5rem;
}
#app .parent .editor .btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
#app .parent .editor input {
border-top-right-radius: 0;
border-top-right-radius: 0;
}
#app .split .child {
width: 50%;
padding: 1rem;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#app .split .child:last-of-type {
border-left: 1px solid #ddd;
}
#app .split .child h2 {
font-weight: 400;
}
</style>



</head>

<body>
<div id="app">
<div class="parent">
vuex实现组件之间的值传递效果
<editor></editor>
</div>
<div class="split">
<sibling-one class="child"></sibling-one>
<sibling-two class="child"></sibling-two>
</div>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vuex.min.js"></script>
<script>


Vue.use(Vuex);

const store = new Vuex.Store({
state: {
// initial state
message: '随便输入点什么'
},

mutations: {
updateMessage(state, payload) {
state.message = payload.message;
}
}
});



Vue.component('editor', {
template: `
<div>
<div class="editor">
<input class="form-control" @keyup.enter="updateMessage()" v-model="input">
<button class="btn btn-primary"
@click="updateMessage()"
>更改</button>
</div>
</div>
`,
data() {
return {
input: store.state.message
};

},
methods: {
updateMessage() {
store.commit('updateMessage', {
message: this.input
});

}
}
});



Vue.component('sibling-one', {
template: `
<div>
<small class="text-muted mb-0">Message from Sibling 1</small>
<h2>{{ message }}</h2>
</div>
`,
computed: {
message() {
return store.state.message;
}
}
});



Vue.component('sibling-two', {
template: `
<div>
<small class="text-muted mb-0">Message on sibling 2</small>
<h2>{{ message }}</h2>
</div>
`,
computed: {
message() {
return store.state.message;
}
}
});



new Vue({
el: '#app'
});

</script>



</body>

</html>

再看一下vuex实现store watch

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
</head>

<body>
<div id="app">
{{ $store.state.n }}
</div>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vuex.min.js"></script>
<script>
const store = new Vuex.Store({
state: {
n: 1
},

getters: {
getN: state => () => state.n
}
});



new Vue({
el: '#app',
store,
mounted() {
setInterval(() => {
this.$store.state.n++;
}, 1000);
this.$store.watch(this.$store.getters.getN, n => {
console.log('watched: ', n);
});
}
});

</script>



</body>

</html>




网友回复

我知道答案,我要回答