vue组件间通信用例
父组件传值给子组件 -- 以封装公用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组件间通信用例的更多相关文章
- vue组件间通信六种方式(完整版)
本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...
- Vue组件间通信6种方式
摘要: 总有一款合适的通信方式. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的 ...
- Vue 组件间传值
前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...
- Vue组件间通信-Vuex
上回说到Vue组件间通讯,最后留了一个彩蛋~~~Vuex.Vuex是另一种组件通讯的方法,这节来说说Vuex(store仓库). 首先Vuex需要安装,安装的方式有很多,在这里就不一一细说了.我是通过 ...
- Vue组件间通信方式
一.Props传递数据 在父组件中使用子组件,本质通过v-bind绑定属性传入子组件,子组件通过props接收父组件传入的属性 <template> <div> 父组件:{{m ...
- webpack+vue 组件间传参(单一事件中心管理组件通信--$root),如果有路由的话会失效
先给一个例子: <body> <div id="box"> <com-a></com-a> <com-b></co ...
- vue 组件间的通信
(1)props:用于父组件向子组件传递消息 使用方法: 在父组件中,使用子组件时,<Child v-bind:data="data"/>,通过v-bind把子组件需要 ...
- vue组件间的通信
组件的定义: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.v ...
- vue组件间通信
组件间通信(父子,兄弟) 相关链接\组件通信http://www.cnblogs.com/xulei1992/p/6121974.html 学习链接Vue.js--60分钟快速入门http://www ...
随机推荐
- python编写微信公众号首图思路详解
前言 之前一直在美图秀秀调整自己的微信公众号首图,效果也不尽如人意,老是调来调去,最后发出来的图片被裁剪了一大部分,丢失部分关键信息,十分恼火,于是想着用python写一个程序,把微信公众号首图的模式 ...
- zookeeper中controller项目中资源配置文件applicationContext.xml配置文件编写
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- JavaWeb学习篇之----Servlet
今天来继续学习JavaWeb的相关知识,之前都是都介绍一些基本知识,从今天开始我们来说一下如何在服务器编写程序,这里就需要来介绍一下Servlet的相关知识了.Servlet就是一个能够运行在服务器端 ...
- NX二次开发-UFUN输入Part的TAG,获取整个部件表达式的TAG和表达式个数UF_MODL_ask_exps_of_part
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_part.h> UF_initialize ...
- hdu多校第五场1004 (hdu6627) equation 1 计算几何
题意: 给你一个C,再给你n组a,b,让你求x取什么值的时候,$ \sum_{i=1}^n |a_i*x+b_i| =C $,要求求出解的个数,并用最简分数从小到大表示,如果有无穷多解,输出-1. 题 ...
- c++ 实现元组 重载cout os 输出
#include <iostream> #include <string> using namespace std; class CAnyType //: public COb ...
- Java-Class-@I:org.springframework.beans.factory.annotation.Autowired
ylbtech-Java-Class-@I:org.springframework.beans.factory.annotation.Autowired 1.返回顶部 2.返回顶部 1. pack ...
- 转载:jQuery 获取屏幕高度、宽度
做手机Web开发做浏览器兼容用到了,所以在网上找了些汇总下. alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height() ...
- 《DSP using MATLAB》Problem 9.2
前几天看了看博客,从16年底到现在,3年了,终于看书到第9章了.都怪自己愚钝不堪,唯有吃苦努力,一点一点一页一页慢慢啃了. 代码: %% ------------------------------- ...
- 跳一跳外挂的python实现--OpenCV步步精深
去我的个人网站看看吧 http://opencvblog.com/跳一跳外挂-python实现/ 都在这里啦