父组件传值给子组件 -- 以封装公用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. No parser no filepath given问题解决

    关于 vue 项目 run dev 的时候,控制台警告: No parser and no filepath given, using 'babylon' the parser now but thi ...

  2. Jmeter-【beanshell处理器】-随机获取手机号

    一.通过操作变量 二.引用外部Java文件 三.引用外部class文件

  3. Java-Class-@I:org.springframework.validation.annotation.Validated

    ylbtech-Java-Class-@I:org.springframework.validation.annotation.Validated 1.返回顶部   2.返回顶部 1. package ...

  4. AtCoder ABC 127F Absolute Minima

    题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f 题目大意 初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b” ...

  5. Git及github使用(二)上传项目

    接上篇中创建好的项目. 1.进入到相应的目录右键Git bash here打开客户端 2.创建一个readme文本 $ echo "# Python日常记录积累" >> ...

  6. Android Telephony分析(三) ---- RILJ详解

    前言 本文主要讲解RILJ工作原理,以便更好地分析代码,分析业务的流程.这里说的RILJ指的是RIL.java (frameworks\opt\telephony\src\java\com\andro ...

  7. Java 并发工具包——ExecutorService常用线程池

    1. 执行器服务 ExecutorService java.util.concurrent.ExecutorService 接口表示一个异步执行机制,使我们能够在后台执行任务.因此一个 Executo ...

  8. 网页开发人员收藏的16款HTML5工具

    本文收集的20款优秀的 HTML5 Web 应用程序,值得添加到您的 HTML5 的工具箱中,他们能够帮助你开发前端项目更快.更容易. Initializr Initializr 是一个可以让你创建 ...

  9. SVN Cannot merge into a working copy that has local modifications

    我尝试了 主支,分支都提交,但是依然无法合并. 最终,我在服务器上将分支删除,然后主支在拷贝过去. 一,打开服务器资源 二,删除分支 三,拷贝主支到分支 四,刷新分支,就能看到了. 然后在分支项目中, ...

  10. log-slave-updates参数

    从库做为其他从库的主库时 log-slave-updates参数是必须要添加的,因为从库要作为其他从库的主库,必须添加该参数.该参数就是为了让从库从主库复制数据时可以写入到binlog日志,为什么要用 ...