Vue的七种传值方式
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.js里new一个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>
如果看了觉得有帮助的,我是@鹏多多,欢迎 点赞 关注 评论;
END

往期文章
个人主页
Vue的七种传值方式的更多相关文章
- Vue 常用三种传值方式
Vue常用的三种传值方式: 父传子 子传父 非父子传值 引用官网一句话:父子组件的关系可以总结为 prop 向下传递,事件向上传递.父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消 ...
- 简述UIViewControl之间的七种传值方式~~~
将自己学习到的UIViewControl之间传值的几种方式在这里做一下总结,希望童鞋们多多支持哈--- 一.正向传值方式 这种方式传值应该是最简单的方式,我们先来建立两个视图控制器暂且称为OneVie ...
- Vue中两种传值方式
第一种:通过url传参,直接在地址后加? ,通过this.$route.query对象获取 第二种:通过路由传参,修改路由,通过this.$route.params对象获取
- Vue中常用的几种传值方式
Vue中常用的几种传值方式 1. 父传子 父传子的实现方式就是通过props属性,子组件通过props属性接收从父组件传过来的值,而父组件传值的时候使用 v-bind 将子组件中预留的变量名绑定为da ...
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- django之创建第7-6-第三种传值方式
1.创建bar.html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- django之创建第7-5-第二种传值方式(time/1232/xiaodneg)
1.修改views文件 def foo(request,myID,myName): t = loader.get_template("foo.html") user = {&quo ...
- 【转】vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
随机推荐
- MySQL select 语句指定字段查询
指定字段查询 SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as al ...
- 【Linux】ntp的一些坑。你肯定遇到过
ntpdate提示 19 Jan 10:33:11 ntpdate[29616]: no server suitable for synchronization found 这种问题从下面几个点开始验 ...
- Apache目录详解
Apache的主要目录和配置文件理解 参考链接:http://httpd.apache.org/docs/2.4/misc/security_tips.html 一.Apache主要配置文件注释(演示 ...
- Java并发包源码学习系列:JDK1.8的ConcurrentHashMap源码解析
目录 为什么要使用ConcurrentHashMap? ConcurrentHashMap的结构特点 Java8之前 Java8之后 基本常量 重要成员变量 构造方法 tableSizeFor put ...
- SpringBoot JPA简单使用
引自B站楠哥:https://www.bilibili.com/video/BV137411B7vB 一.新建Springboot项目 pom.xml文件 <?xml version=&qu ...
- apscheduler(定时任务) 基于redis持久化配置操作
apscheduler(定时任务) 基于redis持久化配置操作 安装模块 pip install apscheduler 导入模块配置 ## 配置redis模块 from apscheduler.j ...
- 基于scrapy框架的分布式爬虫
分布式 概念:可以使用多台电脑组件一个分布式机群,让其执行同一组程序,对同一组网络资源进行联合爬取. 原生的scrapy是无法实现分布式 调度器无法被共享 管道无法被共享 基于 scrapy+redi ...
- MySQL调优之索引优化
一.索引基本知识 1.索引的优点 1.减少了服务器需要扫描的数据量 2.帮助服务器避免排序和临时表 例子: select * from emp orde by sal desc; 那么执行顺序: 所以 ...
- has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
前端显示: has been blocked by CORS policy: Response to preflight request doesn't pass access control che ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...