父组件传值给子组件 -- 以封装公用slide组件为例

父组件

<template>
<section class="banner">
<slide :imgList="imgList" :options="swiperOption" width="100%" height="480"></slide>
</section>
</template> <script>
import Slide from "@/components/slide";
import img1 from "@/assets/1.jpg";
import img2 from "@/assets/2.jpg";
import img3 from "@/assets/3.jpg";
import img4 from "@/assets/4.jpg";
import img5 from "@/assets/5.jpg";
export default {
data() {
return {
swiperOption: {
// swipe官方的api所有参数都可以用
},
imgList: [img1, img2, img3, img4, img5]
};
},
components: {
Slide
}
};
</script>

子组件

<template>
<swiper :options="swiperOption">
<swiper-slide v-for="(item, index) in imgList" :key="index">
<img :src="item" :width="width" :height="height" alt>
</swiper-slide>
<!-- Optional controls -->
<div class="swiper-pagination" slot="pagination"></div>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</template> <script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper"; export default {
components: {
swiper,
swiperSlide
},
props: ["imgList", "options", "width", "height"],
data() {
return {
swiperOption: {
// 所有的参数同 swiper 官方 api 参数
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
},
// 分组模式
// slidesPerView: 3,
// spaceBetween: 30,
// 是否循环
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true
}
}
};
},
computed: {},
created() {
// 合并用户设置的参数
this.swiperOption = Object.assign(this.swiperOption, this.options);
},
mounted() {
// current swiper instance
// 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
//console.log("this is current swiper instance object", this.swiper);
// this.swiper.slideTo(3, 1000, false);
}
};
</script> <style lang='less'>
</style>

子组件传值给父组件

父组件

<template>
<main>
<section class="container thumb">
<p class="lead">{{ msg }}</p>
<thumb @listenToChildEvent="foo"></thumb>
</section>
</main>
</template> <script>
import Thumb from "@/components/thumbnail";
export default {
data() {
return {
msg: "子组件将要修改父组件的值"
};
},
components: {
Thumb
},
methods: {
foo(data) {
console.log(data);
this.msg = data;
}
}
};
</script> <style lang="less">
.thumb {
margin-top: 20px;
}
</style>

子组件

<template>
<div class="row">
<div class="col-sm-6 col-md-3" v-for="(item, index) in 4" :key="index">
<div class="thumbnail">
<img src="@/assets/thumb.svg" alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Vero vitae mollitia quos dolores ullam suscipit temporibus sed ex sapiente quisquam impedit nobis, consectetur sit, earum dolor aspernatur perspiciatis distinctio odio?</p>
<p>
<a href="javascript:;" @click="handleClick(index)" class="btn btn-primary" role="button">修改父组件的值</a>
</p>
</div>
</div>
</div>
</div>
</template> <script>
export default {
methods: {
handleClick(index) {
console.log(index);
this.$emit("listenToChildEvent", "我是第"+ (index + 1) +"个修改父组件值的按钮");
}
}
};
</script> <style>
</style>

vue组件间通信用例的更多相关文章

  1. vue组件间通信六种方式(完整版)

    本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...

  2. Vue组件间通信6种方式

    摘要: 总有一款合适的通信方式. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的 ...

  3. Vue 组件间传值

    前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...

  4. Vue组件间通信-Vuex

    上回说到Vue组件间通讯,最后留了一个彩蛋~~~Vuex.Vuex是另一种组件通讯的方法,这节来说说Vuex(store仓库). 首先Vuex需要安装,安装的方式有很多,在这里就不一一细说了.我是通过 ...

  5. Vue组件间通信方式

    一.Props传递数据 在父组件中使用子组件,本质通过v-bind绑定属性传入子组件,子组件通过props接收父组件传入的属性 <template> <div> 父组件:{{m ...

  6. webpack+vue 组件间传参(单一事件中心管理组件通信--$root),如果有路由的话会失效

    先给一个例子: <body> <div id="box"> <com-a></com-a> <com-b></co ...

  7. vue 组件间的通信

    (1)props:用于父组件向子组件传递消息 使用方法: 在父组件中,使用子组件时,<Child v-bind:data="data"/>,通过v-bind把子组件需要 ...

  8. vue组件间的通信

    组件的定义: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.v ...

  9. vue组件间通信

    组件间通信(父子,兄弟) 相关链接\组件通信http://www.cnblogs.com/xulei1992/p/6121974.html 学习链接Vue.js--60分钟快速入门http://www ...

随机推荐

  1. Android中的SrollView滚动详解

    今天开发遇到一个需求就是ScrollView中嵌套一个ListView,同时需要实现滑动到底部自动加载更多,我们知道ListView滑动到底部简单实现onScrollListener()监听器即可,但 ...

  2. NX二次开发-设置WCS位置UF_CSYS_set_wcs

    NX9+VS2012 UF_initialize(); //输入X向量Y向量输出一个3*3矩阵 ] = {0.0, 0.0, 1.0}; ] = {0.0, 1.0, 0.0}; ]; UF_MTX3 ...

  3. git tag操作

    git tag //查看tag git tag v1.0.0 c809ddbf83939a89659e51dc2a5fe183af384233 //在某个commit 上打tag git push o ...

  4. python list基本操作一

    a = [1,2,3,1,2,3] 一.删除元素 1.按索引删除: a.pop(1) # 删除第二个值 # in:[1,2,3,2] # out:[1,3,2] 返回值:被删除的元素,这个时候list ...

  5. go 数据类型和操作符

    文件名&关键字&标识符 1. 所有go源码以 .go结尾 2. 标识符以字母或者下划线开头,大小写敏感:add, Add, _add _是特殊标识符,用来忽略结果 3. 保留关键字 G ...

  6. C++列表初始化是初始化本类自身含有的成员变量,不能直接初始化继承过来的成员变量

    在构造函数体内赋值就是对的了

  7. 2019-5-21-dotnet-使用-GC.GetAllocatedBytesForCurrentThread-获取当前线程分配过的内存大小...

    title author date CreateTime categories dotnet 使用 GC.GetAllocatedBytesForCurrentThread 获取当前线程分配过的内存大 ...

  8. 使用Echarts的步骤

    Echarts官网地址:https://echarts.baidu.com/index.html 步骤如下: 1.获取Echarts (1)可以在Echarts官网去下载,选择需要的版本下载,根据开发 ...

  9. 数据库MySQL--常见函数

    例子文件:https://files.cnblogs.com/files/Vera-y/myemployees.zip 函数:将一组逻辑语句封装在函数体中,对外暴露函数名 调用:select 函数名( ...

  10. Android开发 SeekBar开发记录

    前言 开发记录博客不是讲解使用博客,更多的是各种功能与点子的记录 基本使用 <SeekBar android:layout_width="match_parent" andr ...