详解Vue的七种传值方式


Posted in Vue.js onFebruary 08, 2021

1,父传子

子组件中定义props字段,类型为数组(如果需要限制字段值类型,也可以定义为对象的形式)。如下图的例子,父组件挂载子组件HelloWorld,在组件标签上给title赋值,子组件HelloWorld定义props,里面有一个值是title,这样子组件就可以使用父组件的值了。

父组件

<template>
 <div>
 <HelloWorld :title="msg" />
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },
 components: {
 HelloWorld,
 },
};
</script>

子组件

<template>
 <div class="hello">
 <h1>{{ title }}</h1>
 </div>
</template>

<script>
export default {
 name: "HelloWorld",
 props:["title"],
 data() {
 return {};
 },
};
</script>

2,子传父

子传父,需要在子组件中触发一个事件,在事件中,调用$emit('父组件的方法名', '传递的值'),然后在父组件中,通过自定义事件接收传递过来的值。

子组件

<template>
 <div class="hello">
 <h1 @click="add">{{ title }}</h1>
 </div>
</template>

<script>
export default {
 name: "HelloWorld",
 props: ["title"],
 data() {
 return {
  age:18
 };
 },
 methods: {
 add(){
  this.$emit("childEvent", this.age);
 }
 },
};
</script>

父组件

<template>
 <div>
 <HelloWorld @childEvent="parentEvent" :title="msg" />
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },

 methods: {
 parentEvent(e) {
  console.log(e);
 },
 },
 components: {
 HelloWorld,
 },
};
</script>

3,兄弟组件传值

1,先新建一个bus.js文件,在bus.jsnew一个Vue实例,充当传输数据的中间层。

import Vue from 'vue';
export default new Vue;

2,在组件A中引入bus.js,通过bus.$emit('事件名','参数')传递参数

<template>
 <div class="hello">
 <h1 @click="add">{{ title }}</h1>
 </div>
</template>

<script>
import bus from "../publicFn/bus.js";

export default {
 name: "HelloWorld",
 props: ["title"],
 data() {
 return {
  age:18
 };
 },
 methods: {
 add(){
  bus.$emit("childEvent", this.age);
 }
 },
};
</script>

3,在B组件mounted周期中使用$on('事件名', function(){})接收

<template>
 <div id='swiper'>
 <button>我是按钮</button>
 </div>
</template>

<script>

import bus from "../publicFn/bus.js";

export default {
 name:'Swiper',
 data (){
 return {

 }
 },
 mounted(){
 bus.$on("childEvent", (e) => {
  console.log(e)
 })
 }
}
</script>

4,父组件使用子组件的数据和方法

1,在子组件标签上写上ref属性

2,父组件通过this.$refs.id.方法名或者this.$refs.id.属性名的方式可以访问子组件。

父组件

<template>
 <div>
 <HelloWorld :title="msg" ref="hello" />
 <button @click="parentEvent">我是父亲</button>
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },

 methods: {
 parentEvent() {
  this.$refs.hello.add();
  console.log(this.$refs.hello.age);
 },
 },
 components: {
 HelloWorld
 },
};
</script>

子组件

<template>
 <div class="hello">
 <h1>{{ title }}</h1>
 </div>
</template>

<script>
export default {
 name: "HelloWorld",
 props: ["title"],
 data() {
 return {
  age:18
 };
 },
 methods: {
 add(){
  console.log("我是子组件");
 }
 },
};
</script>

5,子组件使用父组件的数据和方法

在子组件中,可以使用$parent访问其上级父组件的数据和方法,如果是多重嵌套,也可以使用多层$parent

父组件

<template>
 <div>
 <HelloWorld :title="msg" ref="hello" />
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },

 methods: {
 parentEvent() {
  console.log("我是父组件的方法");
 },
 },
 components: {
 HelloWorld
 },
};
</script>

子组件

<template>
 <div class="hello">
 <h1 @click="add">{{ title }}</h1>
 </div>
</template>

<script>
export default {
 name: "HelloWorld",
 props: ["title"],
 data() {
 return {
  age:18
 };
 },
 methods: {
 add(){
  console.log(this.$parent.msg)
  this.$parent.parentEvent();
 }
 },
};
</script>

6,Vuex传值

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。一般小项目不需要用到。

6.1,定义store

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
 state: {
 school: "清华大学",
 a:"nice"
 },
 getters: {
 returnVal(state) {
  return state.school + state.a;
 },
 },
 mutations: {
 changeSchool(state, val) {
  state.school = val;
  console.log('修改成功');
 },
 },
 actions: {},
 modules: {}
});

6.2,挂载

import Vue from 'vue';
import App from './App.vue';
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import publicFn from "./publicFn/publicFn";

Vue.config.productionTip = false


const url = process.env.VUE_APP_URL;
Vue.prototype.$url = url;
Vue.prototype.$publicFn = publicFn;

Vue.use(ElementUI);

new Vue({
 router,
 store,
 render: h => h(App),
}).$mount('#app')

6.3,使用

<template>
 <div class="hello">
 <h1 @click="add">{{ title }}</h1>
 </div>
</template>

<script>
export default {
 name: "HelloWorld",
 props: ["title"],
 data() {
 return {
  age:18
 };
 },
 methods: {
 add(){
  console.log(this.$store.state.school);//获取值
  //this.$store.commit('changeSchool', '北京大学');//修改值
  // console.log(this.$store.getters.returnVal)//获取过滤后的值
 }
 },
};
</script>

7,路由传值

7.1 通过query传值

注意:该方式刷新页面参数不丢失,并且会在地址栏后将参数显露,http://localhost:9000/#/conter?id=10086&name=%E9%B9%8F%E5%A4%9A%E5%A4%9A

页面A

<template>
 <div>
 <HelloWorld :title="msg" ref="hello" />
 <button @click="parentEvent">跳转</button>
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },

 methods: {
 parentEvent() {
  this.$router.push({
  path:"/conter",
  name:'conter',
  query:{
   id:10086,
   name:"鹏多多"
  }
  })
 },
 },
 components: {
 HelloWorld
 },
};
</script>

页面B

<template>
 <div id='conter'>

 </div>
</template>

<script>

export default {
 name:'conter',
 data (){
 return {

 }
 },
 created (){
 console.log(this.$route.query.id, this.$route.query.name);
 },
}
</script>

7.2 通过params传值

注意:该方式刷新页面参数会丢失,可以接收后存在sessionStorage

A页面

<template>
 <div>
 <HelloWorld :title="msg" ref="hello" />
 <button @click="parentEvent">跳转</button>
 </div>
</template>

<script>
import HelloWorld from "../components/HelloWorld.vue";

export default {
 name: "Home",
 data() {
 return {
  msg: "搜索音乐",
 };
 },

 methods: {
 parentEvent() {
  this.$router.push({
  path:"/conter",
  name:"conter",
  params:{
   id:10086,
   name:"鹏多多"
  }
  })
 },
 },
 components: {
 HelloWorld
 },
};
</script>

B页面

<template>
 <div id='conter'>

 </div>
</template>

<script>

export default {
 name:'conter',
 data (){
 return {

 }
 },
 created (){
 console.log(this.$route.params.id, this.$route.params.name);
 },
}
</script>

到此这篇关于Vue的七种传值方式的文章就介绍到这了,更多相关Vue传值方式内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Vue.js 相关文章推荐
VUE项目实现主题切换的多种方法
Nov 26 Vue.js
vue-calendar-component 封装多日期选择组件的实例代码
Dec 04 Vue.js
Vue如何跨组件传递Slot的实现
Dec 14 Vue.js
vue+element table表格实现动态列筛选的示例代码
Jan 14 Vue.js
Vue实现图书管理案例
Jan 20 Vue.js
vue 动态添加的路由页面刷新时失效的原因及解决方案
Feb 26 Vue.js
Vue中避免滥用this去读取data中数据
Mar 02 Vue.js
详解Vue.js 可拖放文本框组件的使用
Mar 03 Vue.js
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
Apr 17 Vue.js
vue选项卡切换的实现案例
Apr 11 Vue.js
vue的项目如何打包上线
Apr 13 Vue.js
vue-treeselect的基本用法以及解决点击无法出现拉下菜单
Apr 30 Vue.js
Vue中使用wangeditor富文本编辑的问题
Feb 07 #Vue.js
vue使用lodop打印控件实现浏览器兼容打印的方法
Feb 07 #Vue.js
vue如何使用rem适配
Feb 06 #Vue.js
如何管理Vue中的缓存页面
Feb 06 #Vue.js
手动实现vue2.0的双向数据绑定原理详解
Feb 06 #Vue.js
vue3.0 自适应不同分辨率电脑的操作
Feb 06 #Vue.js
vue使用echarts画组织结构图
Feb 06 #Vue.js
You might like
PHP使用json_encode函数时不转义中文的解决方法
2014/11/12 PHP
Laravel 5框架学习之向视图传送数据(进阶篇)
2015/04/08 PHP
php fread函数使用方法总结
2019/05/28 PHP
网站上面有这种切换效果
2006/06/26 Javascript
漂亮的提示信息(带箭头)
2007/03/21 Javascript
摘自启点的main.js
2008/04/20 Javascript
js innerHTML 的一些问题的解决方法
2008/06/22 Javascript
HTML上传控件取消选择
2013/03/06 Javascript
node+express+jade制作简单网站指南
2014/11/26 Javascript
第一次动手实现bootstrap table分页效果
2016/09/22 Javascript
深入理解Angularjs向指令传递数据双向绑定机制
2016/12/31 Javascript
jQuery实现拖拽可编辑模块功能代码
2017/01/12 Javascript
vue中动态添加class类名的方法
2018/09/05 Javascript
vue中组件通信的八种方式(值得收藏!)
2019/08/09 Javascript
js回调函数原理与用法案例分析
2020/03/04 Javascript
详解Vue3中对VDOM的改进
2020/04/23 Javascript
vue3.0实现点击切换验证码(组件)及校验
2020/11/18 Vue.js
基于vuex实现购物车功能
2021/01/10 Vue.js
[59:35]DOTA2-DPC中国联赛定级赛 Aster vs DLG BO3第一场 1月8日
2021/03/11 DOTA
[55:35]DOTA2-DPC中国联赛 正赛 CDEC vs Dragon BO3 第二场 1月22日
2021/03/11 DOTA
详解Python多线程
2016/11/14 Python
对Python 文件夹遍历和文件查找的实例讲解
2018/04/26 Python
python中pytest收集用例规则与运行指定用例详解
2019/06/27 Python
如何使用Python 打印各种三角形
2019/06/28 Python
Python 的AES加密与解密实现
2019/07/09 Python
Python实现子类调用父类的初始化实例
2020/03/12 Python
基于Keras 循环训练模型跑数据时内存泄漏的解决方式
2020/06/11 Python
Django配置Bootstrap, js实现过程详解
2020/10/13 Python
编程输出如下图形
2013/11/24 面试题
物理研修随笔感言
2014/02/14 职场文书
无子女夫妻离婚协议书(4篇)
2014/10/20 职场文书
银行求职信范文怎么写
2015/03/20 职场文书
交通肇事罪辩护词
2015/05/21 职场文书
2015年关爱留守儿童工作总结
2015/05/22 职场文书
毕业生入职感言
2015/07/31 职场文书
Java常用函数式接口总结
2021/06/29 Java/Android