美文网首页
单页面中使用多组件并使用vux通信

单页面中使用多组件并使用vux通信

作者: 手指乐 | 来源:发表于2020-02-03 10:32 被阅读0次
  1. './store/index.js'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        count: 1
    },
    getters: {
        getCount: function(state) {
            return state.count + 1
        }
    },
    mutations: {
        add(state) {
            state.count = state.count + 1
        },
        reduce(state) {
            state.count = state.count - 1
        }
    },
    actions: {
        addAction({ commit }) {
            commit('add');
        }
    }
})

export default store
  1. 'HelloWord'组件:
<template>
  <div class="hello">
    <div>{{this.$store.state.count}}</div>
    <div>{{this.$store.getters.getCount}}</div>
    <button @click="add">加</button>
    <button @click="reduce">减</button>
    <br />
    <router-link to="/Test">显示子组件</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  methods: {
    ...mapActions(["addAction"]),
    add() {
      this.addAction();
    },
    reduce() {
      this.$store.commit("reduce");
    }
  }
};
</script>

<style scoped>
h1,
h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
  1. 路由中配置嵌套子组件
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Test from '@/components/Test'

Vue.use(Router)

export default new Router({
    routes: [{
            path: '/',
            name: 'HelloWorld',
            component: HelloWorld,
            children: [{
                path: '/Test',
                name: 'Test',
                component: Test
            }]
        }
        // {

        // }
    ]
})

路由要配置嵌套子组件,如果直接配置

path: '/Test',
name: 'Test',
component: Test,

会渲染到App.vue中的的最外层组件中,不会渲染到HelloWorld的router-view中

  1. 子组件 'Test'
<template>
  <div>
    <div>test:{{this.$store.state.count}}</div>
    <div>test:{{this.$store.getters.getCount}}</div>
  </div>
</template>

5.在父组件中显示子组件,父组件对vuex中count的增减会同步到子组件

相关文章

  • 单页面中使用多组件并使用vux通信

    './store/index.js' 'HelloWord'组件: 路由中配置嵌套子组件 路由要配置嵌套子组件,如...

  • 钉钉E应用组件的使用

    js中: json中: 使用组件的页面 json 中: 使用组件的页面 axml 中:

  • Vue中父子组件如何进行通信?

    一、父子组件如何进行通信? 父组件向子组件通信使用 props, props定义在子组件中 子组件向父组件通信使用...

  • Vue学习总结(一)

    VUX 安装 需要对 build/webpack.base.conf.js 文件进行配置 使用 需引入并注册组件,...

  • 组件传值

    一. 组件注册 把组件写入公用基础组件中: 二.使用组件 在页面中使用: 三.写组件页面

  • vue 2.0封装model组件

    单文件组件 使用单文件组件封装model的模板、逻辑和样式,之后就可以在页面中调用此通用组件。 需求 model有...

  • 前端常见面试题四

    目录: 1、详述组件通信 2、keep-alive组件的作用 3、单页面应用和多页面应用区别及优缺点 4、什么是计...

  • 移动开发vux组件

    1)vux的组件的介绍 官网 vux是组件式开发其中有许多丰富的组件 以便web移动页面的快速开发 2)vux的使...

  • 自己总结的Angular2组建通信

    组件之间的通信 这里简单的记录自己在angular2中,使用组件通信的一些方法。方便自己以后的使用。 一、组件之间...

  • vue小记

    1.vuwe不仅可以使用单页面开发也可以进行多页面开发2.单页面应用即只有一个html文件,切换界面通过切换组件实...

网友评论

      本文标题:单页面中使用多组件并使用vux通信

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