插槽 vuex getters

插槽 vuex getters

1.插槽

默认插槽

app.vue


<template>
 <div class="container">
  <Category title="美食" >
   <img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
  </Category>

  <Category title="游戏" >
   <ul>
    <li v-for="(g,index) in games" :key="index"></li>
   </ul>
  </Category>

  <Category title="电影">
   <video controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
  </Category>
 </div>
</template>

<script>
 import Category from './components/Category'
 export default {
  name:'App',
  components:{Category},
  data() {
   return {
    foods:['火锅','烧烤','小龙虾','牛排'],
    games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
    films:['《教父》','《拆弹专家》','《你好,李焕英》','《尚硅谷》']
   }
  },
 }
</script>

<style scoped>
 .container{
  display: flex;
  justify-content: space-around;
 }
</style>

Category.vue

<template>
 <div class="category">
  <h3>分类</h3>
  <!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充) -->
  <slot>我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>
 </div>
</template>

<script>
 export default {
  name:'Category',
  props:['title']
 }
</script>

<style scoped>
 .category{
  background-color: skyblue;
  width: 200px;
  height: 300px;
 }
 h3{
  text-align: center;
  background-color: orange;
 }
 video{
  width: 100%;
 }
 img{
  width: 100%;
 }
</style>

具名插槽

作用域插槽

2.vuex

多组件依赖同一状态(就是类似java的类的静态成员变量,实例都可以使用)

来自不同组件需要变更同一状态(多个实例可以使用或者修改该)

所以这个vuex相当于共享变量意思,是一种状态管理模式和库

安装vuex npm -i vuex@3

在main.js 引入vuex,这个$store会挂载在所有得vc中

//引入store
import store from './store'
//创建vm
new Vue({
	el:'#app',
	render: h => h(App),
    //让所有得vc都有$store
	store,
	beforeCreate() {
		Vue.prototype.$bus = this
	}
})

在store文件夹下创建index.js 引入vuex

import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)

2.1 加法例子

main.js

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//引入插件
import vueResource from 'vue-resource'
//引入store
import store from './store'
//关闭Vue的生产提示
Vue.config.productionTip = false
//使用插件
Vue.use(vueResource)

//创建vm
new Vue({
   el:'#app',
   render: h => h(App),
   store,
   beforeCreate() {
      Vue.prototype.$bus = this
   }
})

index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)

//准备actions——用于响应组件中的动作
const actions = {
   /* jia(context,value){
      console.log('actions中的jia被调用了')
      context.commit('JIA',value)
   },
   jian(context,value){
      console.log('actions中的jian被调用了')
      context.commit('JIAN',value)
   }, */
   jiaOdd(context,value){
      console.log('actions中的jiaOdd被调用了')
      if(context.state.sum % 2){
         context.commit('JIA',value)
      }
   },
   jiaWait(context,value){
      console.log('actions中的jiaWait被调用了')
      setTimeout(()=>{
         context.commit('JIA',value)
      },500)
   }
}
//准备mutations——用于操作数据(state)
const mutations = {
   JIA(state,value){
      console.log('mutations中的JIA被调用了')
      state.sum += value
   },
   JIAN(state,value){
      console.log('mutations中的JIAN被调用了')
      state.sum -= value
   }
}
//准备state——用于存储数据
const state = {
   sum:0 //当前的和
}

//创建并暴露store
export default new Vuex.Store({
   actions,
   mutations,
   state,
})

app.vue

<template>
 <div>
  <Count/>
 </div>
</template>

<script>
 import Count from './components/Count'
 export default {
  name:'App',
  components:{Count},
  mounted() {
   // console.log('App',this)
  },
 }
</script>

count.vue

<template>
 <div>
  <h1>当前求和为:</h1>
  <select v-model.number="n">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
  </select>
  <button @click="increment">+</button>
  <button @click="decrement">-</button>
  <button @click="incrementOdd">当前求和为奇数再加</button>
  <button @click="incrementWait">等一等再加</button>
 </div>
</template>

<script>
 export default {
  name:'Count',
  data() {
   return {
    n:1, //用户选择的数字
   }
  },
  methods: {
   increment(){
    this.$store.commit('JIA',this.n)
   },
   decrement(){
    this.$store.commit('JIAN',this.n)
   },
   incrementOdd(){
    this.$store.dispatch('jiaOdd',this.n)
   },
   incrementWait(){
    this.$store.dispatch('jiaWait',this.n)
   },
  },
  mounted() {
   console.log('Count',this)
  },
 }
</script>

<style lang="css">
 button{
  margin-left: 5px;
 }
</style>

代码流程

具体传值方式

组件取值方式

在count组件用插值语法取出 <p></p>

2.2 其他组件取值

另一组件取值 在index.js添加SUBTEN即可

<template>
  <div>
    <p></p>
    <el-button @click="subten">另一组件减十</el-button>
  </div>

</template>

<script>
export default {
  name: "mytest",
  data(){
    return{

    }
  },
  methods:{
    subten(){
      this.$store.commit("SUBTEN",10)
    }
  }

}
</script>

<style scoped>

</style>

2.3 getters

如果展示数据复杂,而且需要复用使用

在mutations加工state的值还需要处理,就需要在store添加一个配置项,这样在每个组件都能用的

在count组件使用 <h3>当前求和放大10倍为:</h3>

2.4 mapState和mapGetters

考虑到插值语法``过长可以使用计算属性替换

computed:{
	bigsum(){
		return this.$store.state.sum
	}
   //借助mapState生成计算属性,从state中读取数据。(对象写法)
  // ...mapState({he:'sum',xuexiao:'school',xueke:'subject'})
   //借助mapState生成计算属性,从state中读取数据。(数组写法)
	//...mapState(['sum','school','subject']),
   //...mapGetters(['bigSum'])
}

这样就可以在视图写这样的插值了

批量的计算属性可以用...mapState({he:'sum',xuexiao:'school',xueke:'subject'})

<p></p>
<p></p>
<p></p>

mapState的值实在state定义的

const state = {
  sum:0, //当前的和
  school:'尚硅谷',
  subject:'前端'
}

相对应的在index.js中的getters 用 ...mapGetters(['bigSum'])

原始的组件调用

现在组件调用

2.5 mapActions与napMutations

同上

<template>
 <div>
  <h1>当前求和为:</h1>
  <h3>当前求和放大10倍为:</h3>
  <h3>我在,学习</h3>
  <select v-model.number="n">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
  </select>
  <button @click="increment(n)">+</button>
  <button @click="decrement(n)">-</button>
  <button @click="incrementOdd(n)">当前求和为奇数再加</button>
  <button @click="incrementWait(n)">等一等再加</button>
 </div>
</template>

<script>
 import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
 export default {
  name:'Count',
  data() {
   return {
    n:1, //用户选择的数字
   }
  },
  computed:{
   //借助mapState生成计算属性,从state中读取数据。(对象写法)
   // ...mapState({he:'sum',xuexiao:'school',xueke:'subject'}),

   //借助mapState生成计算属性,从state中读取数据。(数组写法)
   ...mapState(['sum','school','subject']),

   /* ******************************************************************** */

   //借助mapGetters生成计算属性,从getters中读取数据。(对象写法)
   // ...mapGetters({bigSum:'bigSum'})
   
   //借助mapGetters生成计算属性,从getters中读取数据。(数组写法)
   ...mapGetters(['bigSum'])

  },
  methods: {
   //程序员亲自写方法
   /* increment(){
    this.$store.commit('JIA',this.n)
   },
   decrement(){
    this.$store.commit('JIAN',this.n)
   }, */

   //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)
   ...mapMutations({increment:'JIA',decrement:'JIAN'}),

   //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(数组写法)
   // ...mapMutations(['JIA','JIAN']),

   /* ************************************************* */

   //程序员亲自写方法
   /* incrementOdd(){
    this.$store.dispatch('jiaOdd',this.n)
   },
   incrementWait(){
    this.$store.dispatch('jiaWait',this.n)
   }, */

   //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)
   ...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

   //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(数组写法)
   // ...mapActions(['jiaOdd','jiaWait'])
  },
  mounted() {
   const x = mapState({he:'sum',xuexiao:'school',xueke:'subject'})
   console.log(x)
  },
 }
</script>

<style lang="css">
 button{
  margin-left: 5px;
 }
</style>
//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)

//准备actions——用于响应组件中的动作
const actions = {
   /* jia(context,value){
      console.log('actions中的jia被调用了')
      context.commit('JIA',value)
   },
   jian(context,value){
      console.log('actions中的jian被调用了')
      context.commit('JIAN',value)
   }, */
   jiaOdd(context,value){
      console.log('actions中的jiaOdd被调用了')
      if(context.state.sum % 2){
         context.commit('JIA',value)
      }
   },
   jiaWait(context,value){
      console.log('actions中的jiaWait被调用了')
      setTimeout(()=>{
         context.commit('JIA',value)
      },500)
   }
}
//准备mutations——用于操作数据(state)
const mutations = {
   JIA(state,value){
      console.log('mutations中的JIA被调用了')
      state.sum += value
   },
   JIAN(state,value){
      console.log('mutations中的JIAN被调用了')
      state.sum -= value
   }
}
//准备state——用于存储数据
const state = {
   sum:0, //当前的和
   school:'尚硅谷',
   subject:'前端'
}
//准备getters——用于将state中的数据进行加工
const getters = {
   bigSum(state){
      return state.sum*10
   }
}

//创建并暴露store
export default new Vuex.Store({
   actions,
   mutations,
   state,
   getters
})

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦