zoukankan      html  css  js  c++  java
  • vue 父子组件传值

        1.通过npm加载vuex,创建store.js文件,然后在main.js中引入,store.js文件代码如下

     import Vue from 'vue'
    import Vuex from 'vuex'
    Vue.use(Vuex);
    const state = {
    author:'Wise Wang'
    };
    const mutations = {
    newAuthor(state,msg){
    state.author = msg
    }
    }
    export default new Vuex.Store({
    state,
    mutations
    })
    2、父组件可以使用 props 把数据传给子组件(props是单向绑定的)。
    <div id="app">
         <div>这是父组件的parentArray:{{parentArray}}</div>
         <my-component :child-array="parentArray"></my-component>
       </div>
       <script>
         Vue.component('my-component', {
           template: `
           <div>这是接收了父组件传递值的子组件的childArray: {{childArray}} <br>
               <button type="button" @click="changeArray">
               点击我改变父元素的parentArray</button>
             </div>`,
           props: ['childArray'],
           data () {
             return {
               counter: 1
             }
           },
           methods: {
             changeArray () {
               this.childArray.push(this.counter++)
             }
           }
         })
         new Vue({
           el: '#app',
           data: {
             parentArray: []
           }
         })
       </script>


    3、子组件可以使用 $emit 触发父组件的自定义事件。

    vm.$emit( event, arg ) //触发当前实例上的事件

    vm.$on( event, fn );//监听event事件后运行 fn;

    例如:子组件:

        <template>
          <div class="train-city">
            <h3>父组件传给子组件的toCity:{{sendData}}</h3>
            <br/><button @click='select(`大连`)'>点击此处将‘大连’发射给父组件</button>
          </div>
        </template>
        <script>
          export default {
            name:'trainCity',
            props:['sendData'], // 用来接收父组件传给子组件的数据
            methods:{
              select(val) {
                let data = {
                  cityname: val
                };
                this.$emit('showCityName',data);//select事件触发后,自动触发showCityName事件
              }
            }
          }
        </script>

    父组件:

        <template>
            <div>
                <div>父组件的toCity{{toCity}}</div>
                <train-city @showCityName="updateCity" :sendData="toCity"></train-city>
            </div>
        <template>
        <script>
          import TrainCity from "./train-city";
          export default {
            name:'index',
            components: {TrainCity},
            data () {
              return {
                toCity:"北京"
              }
            },
            methods:{
              updateCity(data){//触发子组件城市选择-选择城市的事件
                this.toCity = data.cityname;//改变了父组件的值
                console.log('toCity:'+this.toCity)
              }
            }
          }
        </script>
  • 相关阅读:
    [PHP] 小数转科学计数法, 小数保留 n 位
    [Blockchain] Cosmos Starport 101
    [Blockchain] Cosmos Starport 地址前缀的变更方式
    [Blockchain] Cosmos Starport 安装的三种方式
    [ML] 机器学习的 7 步走
    [FAQ] MEMORY ALLOC FAILED: mmap with HUGETLB failed, attempting without it (you should fix your kernel)
    [FAQ] FastAdmin epay 微信公众号支付 JSAPI 支付必须传 openid ?
    [TP5] 动态绑定指定默认模块, 解决: 控制器不存在:appindexcontrollerApi
    [TP5] ThinkPHP 默认模块和单模块的设置方式
    [TP5] 浅谈 ThinkPHP 的 Hook 行为事件及监听执行
  • 原文地址:https://www.cnblogs.com/liang715200/p/12036259.html
Copyright © 2011-2022 走看看