1. 程式人生 > 其它 >一篇文章帶你深入淺出Vuex

一篇文章帶你深入淺出Vuex

在寫Vuex之前,我們先用一個簡單的例子來實現一個小demo

大家都知道Vue的父傳子用在很多場景,比如像這樣:

父元件:

 <template>
  <div id="app">
    <product-list-one :msg='msg'></product-list-one>  
  </div>
  </template>
  <script>
  export default {
  name: 'app',
  components:{
    'product-list-one':ProductListOne,
  },
  data () {
    return {
      msg:'love'
    }
  }
}
</script>

子元件:

<template>
  <div id="product-list-one">
    <div>{{msg}}</div>
  </div>
</template>
<script>
props:['msg']
</script>

有父傳子,當然也有子傳父:

子元件:

<template>
  <div id="product-list-one">
    <button @click="but">點選</button>
  </div>
</template>
<script>
  data(){
    return {
      txt:'hello'
    }
  },
  methods:{
    but() {
      this.$emit('get',this.txt);
    }
  }
</script>

父元件:


<template>
  <div id="app">
    <product-list-one :msg='msg' @get='world'></product-list-one>  
  </div>
</template>
<script>
import ProductListOne from './components/ProductListOne.vue'
export default {
  name: 'app',
  components:{
    'product-list-one':ProductListOne
  },
  methods:{
    world(data){
      console.log(data)
    }
  }
}
</script>

更多內容請見原文,原文轉載自:https://blog.csdn.net/weixin_44519496/article/details/119250852