父组件传值给子组件 -- 以封装公用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. 深入浅出的Object.defineProperty()

    vue的原理 观察者模式和订阅-发布者模式. Vue实例被创建时,会遍历data属性,并通过Object.defineProperty将 这些属性转化为getter/setter,并进行追踪依赖.每当 ...

  2. 2017-3-8 html基础标签

    <head></head>头标签 <title>页面标签</title> <body>文档的内容可在浏览器中显视的</body> ...

  3. django入门 -- 简单流程

    django入门 -- 简单流程 简介 通过简单示例,使用django完成基本流程的开发,学习django的主要的知识点,在后续课程中会逐个知识点进行深入讲解 以“图书-英雄”管理为示例 主要知识点介 ...

  4. yolov1代码阅读

    yolov1使用的backbone是由GoogLeNet启发而来,有24个卷积层,最后接2个全连接层,详细结构如下图: 检测网络的输入分辨率是448X448,最后的特征图大小为7X7.在特征图的每一个 ...

  5. ionic-CSS:ionic 表单和输入框

    ylbtech-ionic-CSS:ionic 表单和输入框 1.返回顶部 1. ionic 表单和输入框 list 类同样可以用于 input 元素.item-input 和 item 类指定了文本 ...

  6. CodeForces-1221A-2048 Game-思维题

    You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every int ...

  7. oracle 管理表空间

    表空间:是oracle数据库中最大的逻辑存储结构,与操作系统中的数据文件相对应,用于存储数据库中用户创建的所有内容 表空间>数据文件 4.1创建一个永久性表空间myspace create ta ...

  8. Selenium(一)---Selenium的安装和使用

    一.前言 最近在帮一个老师爬取网页内容,发现网页是动态加载的,为了拿到全部的网页数据,这里使用到了Selenium.Selenium 是一个用于Web应用程序测试的工具,它可以模拟真实浏览器,支持多种 ...

  9. 2018-12-22-WPF-在绑定表达式添加计算

    title author date CreateTime categories WPF 在绑定表达式添加计算 lindexi 2018-12-22 16:12:56 +0800 2018-12-22 ...

  10. kerberos协议介绍

    一.kerberos认证过程: client向kerberos服务请求,希望获取访问server的权限.kerberos得到了这个消息,首先得判断client是否是可信赖的,也就是白名单黑名单的说法. ...