Vue之组件间传值
标签: Vue
Vue之父子组件传值
- 父向子传递通过props
- 子向父传递通过$emit
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值</title>
<script src="./bower_components/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<counter :count="num1" @add="handAddTotal"></counter>
<counter :count="num2" @add="handAddTotal"></counter>
求和:{{total}}
</div>
<script>
//自定义组件
var counter = {
props:['count'],//通过属性由父向子传值
data: function() {
return {
number: this.count//子组件内接收父级传过来的值
}
},
template: '<div @click="handleClick">{{number}}</div>',
methods: {
handleClick: function() {
this.number ++;
//通过向外触发事件由子级向父级传值
this.$emit('add',1);
}
}
};
var vm = new Vue({
el: '#app',
//组件注册
components: {
counter
},
data:{
num1:1,
num2:2,
total: 3
},
methods:{
//求和
handAddTotal:function(step){
this.total += step;
}
}
});
</script>
</body>
</html>
注意事项:
- props传过来值,根据单向数据流原则子组件不可直接拿过来修改,可以在子组件的data里定义一个变量转存再来做修改
- 为了保证各组件数据的独立性,子组件的data应该以一个函数返回一个对象的形式来定义,见上示例代码23行
Vue之非父子组件传值
- 通过bus方式传递,也可以叫总线/发布订阅模式/观察者模式
- 通过vuex传递
bus/总线/发布订阅模式/观察者模式演示地址
vuex演示地址
bus方式示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值(bus/总线/发布订阅模式/观察者模式)</title>
<script src="./bower_components/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<child-one content="hello"></child-one>
<child-two content="world"></child-two>
</div>
<script>
Vue.prototype.bus = new Vue();
//自定义组件
var childOne = {
//通过属性由父向子传值
props:{
'content':String
},
data:function(){
return {
contentIn:this.content
}
},
template: '<div @click="handleClick">{{contentIn}}</div>',
methods: {
handleClick: function() {
//通过触发事件向各组件发送数据
this.bus.$emit('change',this.contentIn);
}
},
mounted:function () {
var that = this;
//组件接收事件
this.bus.$on('change',function(val){
that.contentIn = val;
});
}
};
//自定义组件
var childTwo = {
//通过属性由父向子传值
props:{
'content':String
},
data:function(){
return {
contentIn:this.content
}
},
template: '<div @click="handleClick">{{contentIn}}</div>',
methods: {
handleClick: function() {
//通过触发事件向各组件发送数据
this.bus.$emit('change',this.contentIn);
}
},
mounted:function () {
var that = this;
//组件接收事件
this.bus.$on('change',function(val){
that.contentIn = val;
});
}
};
var vm = new Vue({
el: '#app',
//注册组件
components: {
childOne,
childTwo
}
});
</script>
</body>
</html>
vuex方式示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值(vuex)</title>
<script src="./bower_components/vue/dist/vue.js"></script>
<script src="./bower_components/vuex/dist/vuex.js"></script>
</head>
<body>
<div id="app">
<child-one></child-one>
<child-two></child-two>
</div>
<script>
Vue.use(Vuex);
var store = new Vuex.Store({
state: {
count:0
},
mutations: {
increment:function(state){
console.log(123);
state.count++;
}
},
actions: {
increment:function(context){
context.commit('increment')
}
},
getters: {
getCount:function(state){
return state.count;
}
}
});
//自定义组件
var childOne = {
computed: {
countIn:function(){
return store.getters.getCount
}
},
template: '<div @click="handleClick">{{countIn}}</div>',
methods: {
handleClick: function() {
store.dispatch('increment');
}
}
};
//自定义组件
var childTwo = {
computed: {
countIn:function(){
return store.getters.getCount
}
},
template: '<div @click="handleClick">{{countIn}}</div>',
methods: {
handleClick: function() {
store.dispatch('increment');
}
}
};
var vm = new Vue({
el: '#app',
store,
//注册组件
components: {
childOne,
childTwo
}
});
</script>
</body>
</html>
附上vuex官网地址:https://vuex.vuejs.org/zh/
Vue之组件间传值的更多相关文章
- Vue中组件间传值常用的几种方式
版本说明: vue-cli:3.0 一.父子组件间传值 1.props/$emit -父组件==>>子组件: 子组件中通过定义props接收父组件中通过v-bind绑定的数据 父组件代码 ...
- Vue学习(二)-Vue中组件间传值常用的几种方式
版本说明:vue-cli:3.0 主要分为两类: 1.父子组件间的传值 2.非父子组件间的传值 1.父子组件间传值 父组件向子组件传值 第一种方式: props 父组件嵌套的子组件中,使用v-bind ...
- vue——父子组件间传值
(1)父组件给子组件传值(商品详情页): 根据订单类型,判断显示立即购买/立即拼单: 通过props来传递参数 父组件(商品详情页) 父组件调用子组件,在子组件的标签中,通过:数据名称=”数据”的形式 ...
- Vue 组件间传值
前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...
- vue组件定义方式,vue父子组件间的传值
vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- Vue中组件间通信的方式
Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...
- vue父子组件之间传值
vue父子组件进行传值 vue中的父子组件,什么是父组件什么是子组件呢?就跟html标签一样,谁包裹着谁谁就是父组件,被包裹的元素就是子组件. 父组件向子组件传值 下面用的script引入的方式,那种 ...
- vue父子组件的传值总结
久违的博客园我又回来了.此篇文章写得是vue父子组件的传值,虽然网上已经有很多了.写此文章的目的就是记录下个人学习的一部分.接下来我们就进入主题吧! 在开发vue项目中,父子组件的传值是避免不掉的. ...
- react组件间传值详解
一.父子组件间传值 <1>父传子 父组件:
随机推荐
- [51nod1220] 约数之和(杜教筛+莫比乌斯反演)
题面 传送门 题解 嗯--还是懒得写了--这里 //minamoto #include<bits/stdc++.h> #define R register #define IT map&l ...
- flink学习笔记-快速生成Flink项目
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- P1402 酒店之王 最大流
\(\color{#0066ff}{ 题目描述 }\) XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该 ...
- win10系统重装
问题描述 win10开启热点网卡坏了,没折腾好.然后把系统网卡折腾坏了. 所以重装了系统,写下我的环境从零到晚上的过程 1安装系统 用WePE安装win10,镜像采用:cn_windows_10_en ...
- Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryDa ...
- python安装出现的证书问题
1. pip install pyenv 安装时出现下图错误 Could not install packages due to an EnvironmentError: HTTPSConnectio ...
- C#后端调用WebApi地址
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_对象的相等性和同一性
[重写Equals注意的事项] 1. Equals 必须是自反的:--x.Equals(x)肯定为 true 2. Equals 必须是对称的:--x.Equals(y)肯定返回与y.Equals(x ...
- Django之auth模块(用户认证)登陆组件
auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...
- IP 地址子网划分
1.1 IP地址子网划分 1)容易造成地址浪费 2)容易产生严重的广播风暴 3)会造成路由器转发压力过大 1.2 庞大的网段需要进行子网划分 1)可以有效避免地址浪费 2)有效减少广播风暴的产 ...