Vue(8)--父子组件传值&第三方组件库

一、父子组件传值

1、父传子

$parent:可以获取父组件的实例,还可以继续获取它的父级组件。

root:获取根组件对象。(例如:this.parent.parent.house可以直接写this.root.house)

<div id="app">
    <Child1></Child1>
    <Child2></Child2>
    <Child3></Child3>
</div>
// Child1组件
Vue.component('Child1', {
    // 通过$parent返回的是父组件对象
    template: `
      <div class="child1">
        <h3>房产信息</h3>
         <p>{{$parent.house.address}}</p>
         <p>{{$parent.house.size}}</p>
         <Childson></Childson>
      </div>
    `
})
// childSon组件
Vue.component('Childson',{
    // $parent获取父组件对象;$root获取根组件对象
    template:`
        <div class="childSon">
            <h3>房产信息</h3>
            <p>{{$parent.$parent.house.address}}</p>
            <p>{{$parent.$parent.house.size}}</p>
            <hr>
            <p>{{$root.house.address}}</p>
            <p>{{$root.house.size}}</p>
        </div>
    `
})
// Child2组件
Vue.component('Child2', {
    template: `
      <div class="child2">
        <h3>汽车信息</h3>
         <p>{{$parent.car.name}}</p>
         <p>{{$parent.car.color}}</p>               
      </div>
    `
})
// Child3组件
Vue.component('Child3', {
    template: `
      <div class="child3">
        <h3>存款信息</h3>
         <p>{{$parent.money.value}}</p>
         <p>{{$parent.money.bank}}</p>                              
      </div>
    `
})
// 暂且将#app对应的内容当成根组件
new Vue({
    el: '#app',
    data: {
        house: {
            address: '朝阳区朝阳山庄6栋3单元101',
            size: '150平'
        },
        car: {
            name: '奔驰S400',
            color: '黑色'
        },
        money: {
            value: '150W',
            bank: '中国建设银行'
        }
    }
})

2、子传父

children:返回的是所有子组件对象的数组,再通过下标获取指定的子组件。当组件顺序不会发生变化时,用children;否则用refs。refs:返回的是一个对象,对象中包含所有带有ref属性的子组件。 注意:不是只有组件才可以添加ref属性,任何标签都可以加ref属性 。

<div id="app">
    <!-- 给组件标签,添加ref属性,可以通过$refs对象获取 -->
    <Child1 ref="son1"></Child1>
    <Child2 ref="son2"></Child2>
    <Child3 ref="son3"></Child3>
    <div class="son">
        <h3>大儿子</h3>
        <p>姓名:{{son1.name}}</p>
        <p>年龄:{{son1.age}}</p>
    </div>
    <div class="son">
        <h3>二儿子</h3>
        <p>姓名:{{son2.name}}</p>
        <p>年龄:{{son2.age}}</p>
    </div>
    <div class="son">
        <h3>小儿子</h3>
        <p>姓名:{{son3.name}}</p>
        <p>年龄:{{son3.age}}</p>
    </div>
</div>
// Child1组件
Vue.component('Child1', {
    // 通过$parent返回的是父组件对象
    template:`
        <div class="child1">
            <h3>房产信息</h3>
            <p>{{$parent.house.address}}</p>
            <p>{{$parent.house.size}}</p>
            <Childson></Childson>
        </div>
    `,
    data() {
        return {
            name:'张远',
            age:25
        }
    }
})

// childSon组件
Vue.component('Childson',{
    // $parent获取父组件对象
    // $root获取根组件对象
    template:`
        <div class="childSon">
            <h3>房产信息</h3>
            <p>{{$parent.$parent.house.address}}</p>
            <p>{{$parent.$parent.house.size}}</p>
            <hr>
            <p>{{$root.house.address}}</p>
            <p>{{$root.house.size}}</p>
        </div>
    `
})

// Child2组件
Vue.component('Child2', {
    template:`
        <div class="child2">
            <h3>汽车信息</h3>
            <p>{{$parent.car.name}}</p>
            <p>{{$parent.car.color}}</p>
        </div>
    `,
    data() {
        return {
            name:'张飞',
            age:23
        }
    }
})

Vue.component('Child3', {
    template:`
        <div class="child3">
            <h3>存款信息</h3>
            <p>{{$parent.money.value}}</p>
            <p>{{$parent.money.bank}}</p>
        </div>
    `,
    data() {
        return {
            name:'张强',
            age:20
        }
    }
})

// 暂且将#app对应的内容当成根组件
new Vue({
    el:'#app',
    //数据
    data:{
        house:{
            address:'名城世家3栋1单元1101室',
            size:'150平米'
        },
        car:{
            name:'奔驰S400',
            color:'黑色'
        },
        money:{
            value:'150W',
            bank:'中国建设银行'
        },
        //接收子组件的数据的对象
        son1:{
            name:'',
            age:0
        },
        son2:{
            name:'',
            age:0
        },
        son3:{
            name:'',
            age:0
        }
    },
    mounted() {
        // $children返回的是所有子组件对象的数组
        // 如果页面的结构出现了调整,这里获取的具体的组件下标就对不上号了。
        // console.log(this.$children);
        /* this.son1.name = this.$children[0].name
        this.son1.age = this.$children[0].age
        this.son2.name = this.$children[1].name
        this.son2.age = this.$children[1].age
        this.son3.name = this.$children[2].name
        this.son3.age = this.$children[2].age */

        // $refs返回的是一个对象,对象中包含所有带有ref属性的组件
        // console.log(this.$refs);
        this.son1.name = this.$refs.son1.name
        this.son1.age = this.$refs.son1.age
        this.son2.name = this.$refs.son2.name
        this.son2.age = this.$refs.son2.age
        this.son3.name = this.$refs.son3.name
        this.son3.age = this.$refs.son3.age
    }
})

二、第三方组件

1、element-ui组件库

官网地址:https://element.eleme.cn/#/zh-CN/component/tabs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>常用的第三方组件库</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
    <div id="app">
        <div>
            <el-button type="primary">主要按钮</el-button>
            <el-checkbox label="复选框 A"></el-checkbox>
            <span class="demonstration">默认</span>
            <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="date" label="日期" width="180">
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="address" label="地址">
                </el-table-column>
            </el-table>
            
        </div>
    </div>
    <script src='https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js'></script>
    <!-- 第三方的组件库,必须在vue的下面引入 -->
    <!-- 引入element-vi组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        Vue.config.productionTip = false
        new Vue({
            el: '#app',
            data() {
                return {
                    pickerOptions: {
                        shortcuts: [{
                            text: '最近一周',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '最近一个月',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                                picker.$emit('pick', [start, end]);
                            }
                        }, {
                            text: '最近三个月',
                            onClick(picker) {
                                const end = new Date();
                                const start = new Date();
                                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                                picker.$emit('pick', [start, end]);
                            }
                        }]
                    },
                    value1: '',
                    value2: '',
                    tableData: [{
                        date: '2016-05-02',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1518 弄'
                    }, {
                        date: '2016-05-04',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1517 弄'
                    }, {
                        date: '2016-05-01',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1519 弄'
                    }, {
                        date: '2016-05-03',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1516 弄'
                    }],
                };
            },
        })
    </script>
</body>

</html>

2、iview组件库

官网地址:http://v1.iviewui.com/docs/guide/install

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第三方组件库iview</title>
    <!-- 引入view的样式 -->
    <link rel="stylesheet" href="//unpkg.com/view-design/dist/styles/iview.css">
</head>
<body>
    <div id="app">
        <i-button type="primary">Primary</i-button>
        
    </div>
    <!-- 引入vue -->
    <script type="text/javascript" src="http://vuejs.org/js/vue.min.js"></script>
    <!-- 引入view -->
    <script type="text/javascript" src="http://unpkg.com/view-design/dist/iview.min.js"></script>
    <script>
        new Vue({
            el:'#app',
            
        })
    </script>
</body>
</html>

3、Vant组件库

官网地址:https://vant-contrib.gitee.io/vant/#/zh-CN/

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第三方组件库Vant</title>
    <!-- 引入样式文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.12/lib/index.css" />
</head>

<body>
    <div id="app">
        <van-button type="primary">主要按钮</van-button>
        <van-cell-group>
            <van-cell title="单元格" value="内容" />
            <van-cell title="单元格" value="内容" label="描述信息" />
          </van-cell-group>
          <van-icon name="https://b.yzcdn.cn/vant/icon-demo-1126.png" />
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vant@2.12/lib/vant.min.js"></script>
    <script>
        new Vue({
            el:'#app',
        })
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容