state

作者: 第三人称i | 来源:发表于2018-12-01 12:51 被阅读0次

state 储存数据

<!-- 1. 在store 文件下 创建 state.js  -->
    目录:
        store
        |-- modules
        |-- index.js
        |-- state.js

<!-- 2. state.js ------------- -->
<script>
    const state = {
        Title: "Vuex state"
    }
    export default state;
</script>

<!-- 3. index.js --------------- -->
<script>
    // ...
    import state from "./state.js"
    // ...
    const store =  new Vuex.Store({
        // ...
        state               // state => state: state
    })

    export default store;
</script>

<!-- 4. 组件中使用: -->

<script>
    export default {
        // 计算属性
        computed: {
            Title(){
                return this.$store.state.Title
            }
        }
    }
</script>

<!-- 5. mapState 辅助函数 -->

<script>
    import { mapState } from 'vuex'

    export default {
        computed: {
            ...mapState({
                Title: state => state.Title,
                
                // 传字符串参数 'Title' 等同于 `state => state.Title`
                TitleContent: 'Title'

            })
        }
    }
</script>

相关文章

网友评论

      本文标题:state

      本文链接:https://www.haomeiwen.com/subject/ciglcqtx.html