vue--08 vuex和传值

1. 组件传值

1.1 进入页面即刻传值

// 父组件 Home.vue
import vue09 from '@/components/vue09.vue';
import { provide, reactive } from 'vue';
export default {
  name: 'Home',
  components: {
    vue09
  },
  setup() {
    const p = reactive({ name: 'zhangsan', age: 50, company: 'Dmall' });
    provide('name', p); //传
    return { p };
  }
};

// 子组件 vue09.vue
import { inject } from 'vue';
export default {
  setup() {
    const p = inject('name'); // 收
    return { p };
  }
};

1.2 点击传值

// Home.vue
<template>
  <div class="home">
    这是父组件
    <h2>{{ p.name }}</h2>
    <h2>{{ p.age }}</h2>
    <button @click="func">点击传值给子组件</button>
  </div>
  <vue09 ref="val" />
</template>

<script>
import vue09 from '@/components/vue09.vue';
import { reactive, ref } from 'vue';
export default {
  name: 'Home',
  components: {
    vue09
  },
  setup() {
    const val = ref();
    const p = reactive({ name: 'zhangsan', age: 50, company: 'Dmall' });
    const func = () => {
      val.value.receive(p);
    };

    return { p, func, val };
  }
};
</script>

<style scoped>
.home {
  background-color: red;
  height: auto;
}
</style>
//vue09.vue
<template>
  <div class="new">
    这是子组件
    <!-- <h1>姓名:{{ p }}</h1> -->
  </div>
</template>

<script>
// import reactive from 'vue';
export default {
  setup() {
    // 被父组件调用
    function receive(val) {
      console.log(val);
    }

    return { receive };
  }
};
</script>

<style scoped lang="scss">
.new {
  background-color: blue;
  height: auto;
}
</style>

2. 封装

// src/config/public.js

// 公用的数据或者方法池

import { reactive } from 'vue';

const plblic = () => {
  const info = reactive({
    name: 'zhangsan',
    age: 18
  });
  return { info };
};

export default plblic;
<template>
  <div class="new">
    <h1>子组件</h1>
    <h1>姓名:{{ p1.name }}</h1>
    <h1>年龄:{{ p1.age }}</h1>
    <!-- 修改 -->
    <button @click="p1.name = 'lisi'">button3</button>
  </div>
</template>

<script>
import plblic from '../config/public';
export default {
  setup() {
    const p1 = plblic().info;

    return { p1 };
  }
};
</script>

3. vuex

  • 组件传值
  • 异步调用不能直接修改数据,需要使用同步函数
  • 可以直接使用同步修改数据
<template>
  <div class="new">
    这是子组件
    <h1>姓名:{{ res }}</h1>
    <button @click="func">button</button>
  </div>
</template>

<script>
import { computed } from 'vue';
import { useStore } from 'vuex';
export default {
  setup() {
    // 从vuex数据仓库里取数据
    const store = useStore();

    const res = computed(() => {
      console.log(store.state.name);
      return store.state.name;
    });

    // 点击调用vuex并且改变仓库里的数据
    const func = () => {
      // 异步调用:dispatch
      store.dispatch('sub', '张朝阳');
      // 同步调用:commit
      // store.commit('trigger', '刘强东');
    };

    return { res, func };
  }
};
</script>

<style scoped lang="scss">
.new {
  background-color: blue;
  height: auto;
}
</style>

// store/index.js

import { createStore } from 'vuex';

export default createStore({
  // 创建数据仓库
  state: { name: 'zhangsan', age: 50, company: 'Dmall' },
  // 同步调用
  mutations: {
    trigger(state, val) {
      state.name = val;
    }
  },
  // 异步调用
  // 异步调用不能直接修改数据
  actions: {
    sub(store, val) {
      store.commit('trigger', val);
    }
  }
  // modules: {}
});
// main.js

import store from './store';

createApp(App)
  .use(store)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中使用Vuex进行状态管理是非常常见的,它可以帮助我们更好地组织和管理应用程序的状态。下面是一个简单的例子,演示如何在Vue中使用Vuex进行传值。 1. 安装Vuex 首先,我们需要安装Vuex。可以使用npm或yarn来安装: ``` npm install vuex --save ``` 或者 ``` yarn add vuex ``` 2. 创建store 在Vue中使用Vuex需要先创建一个store。在store中,我们可以定义和管理应用程序的所有状态。 ``` import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ } } }); export default store; ``` 在这个例子中,我们定义了一个名为“count”的状态和一个名为“increment”的mutation,当我们调用“increment”mutation时,它会增加“count”状态的值。 3. 在Vue组件中使用store 现在我们已经创建了一个store,接下来让我们在Vue组件中使用它。我们需要使用Vuex提供的“mapState”和“mapMutations”辅助函数来访问store中的状态和mutations。 ``` <template> <div> <h1>{{ count }}</h1> <button @click="increment">Increment</button> </div> </template> <script> import { mapState, mapMutations } from 'vuex'; export default { computed: { ...mapState(['count']) }, methods: { ...mapMutations(['increment']) } } </script> ``` 在这个例子中,我们使用了“mapState”辅助函数来将“count”状态映射到组件的计算属性中,并使用“mapMutations”辅助函数将“increment”mutation映射到组件的方法中。这样,在组件中就可以直接使用“count”状态和“increment”mutation了。 以上就是使用Vuex进行状态管理的基本过程。在实际开发中,我们通常需要定义更多的状态和mutations,并使用actions和getters等高级特性来管理更复杂的状态。但基本的使用方法和原理都是相似的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值