使用vuex实现父组件调用子组件方法
曲线救国。
核心原理就是父子共用一个vuex对象,且看代码:
父组件parent.vue
<template>
<div class="wrap">
<form action="">
<input type="text" v-model="searchParam.name">
<input type="text" v-model="searchParam.id">
<button @click="search"></button>
</form>
<child></child>
</div>
</template> <script>
import store from '@/vuex'; export default {
name: 'parent',
store,
components: {
'child': () => import('./child.vue'),
},
data () {
return this.$store.state.parent;
},
methods: {
search () {
this.$store.dispatch('search');
}
}
};
</script> <style lang="less" scoped> </style>
子组件 child.vue
<template>
<ul v-if="list && list.length">
<li class="river-item" v-for="item in list">{{item}}</li>
</ul>
</template> <script> export default {
name: 'child',
created () {
this.$store.dispatch('getData');
},
data() {
return this.$store.state.child;
}
};
</script> <style lang="less" scoped> </style>
vuex.js
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex); export default new Vuex.Store({
state: {
parent: {
searchParam: {
name: '',
id: ''
}
},
child: {
pageIndex: 1,
pageTotal: 0
list: []
}
},
actions: {
// 父组件的搜索方法
search({commit, dispatch, state}) {
// 重置子组件的列表
state.child.pageIndex = 1;
state.child.list = [];
dispatch('getData');
}
// 子组件的获取数据方法
getData ({commit, dispatch, state}) {
fetch('http://test.com', {
method: 'POST',
// 使用父组件的参数(连传递props都省了 -_-!)
body: state.parent.searchParam
}).then(res => res.json()).then(data => {
state.child.list = data;
});
} }
});
父组件和子组件的data及method都写到vuex里面去了,数据共享,这样父组件就可以调用vuex里面的action来修改子组件的data了!
使用vuex实现父组件调用子组件方法的更多相关文章
- vue 父组件调用子组件内置方法
背景介绍:外派到泰康做项目.这个项目中有个选择组织的功能,是一个树桩结构的懒加载,于是我就element-ui的tree组件封装了一个公共的组件. 但是后来发现他们的公司组织结构不是都请求的同一个接口 ...
- Vue 父组件调用子组件的方法
qwq 前两天看了下vue,父子组件方法的调用,怕忘记,所以做个小记录. 一.父组件调用子组件的方法 1.父组件 <template> <div id="rightmen ...
- vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等
vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...
- react 父组件调用子组件方法
import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...
- 父组件调用子组件 viewChild
父组件调用子组件 1.在子组件的ts中声明一个变量 public lineout:any="你好,我是被父组件调用的子组件"; 2.在父组件的html中写入 (引入子组件) & ...
- vue父组件调用子组件方法、父组件向子组件传值、子组件向父组件传值
一.父组件调用子组件方法 父组件代码 parent.vue <template> <div> <button @click="parentFun" ...
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
- vue中子组件调用父组件里面的数据和方法 父组件调用子组件的数据和方法
1.子组件直接调用父组件的数据和方法 在父组件father,vue <template> <div> <!-- 父组件里面的数据 --> <p>父组件里 ...
- Vue3 父组件调用子组件的方法
Vue3 父组件调用子组件的方法 // 父组件 <template> <div> 父页面 <son-com ref="sonRef"/> < ...
- React 父组件调用子组件的方法
父组件调用子组件的方法 React v16.3.0 及以后版本使用 import React, {Component} from 'react'; export default class Paren ...
随机推荐
- 自定义Notification实现例子
1.自定义view: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- Tomcat + solr5.2.1环境搭建
1. 下载solr并解压后的目录为:E:\solr-5.2.1 , http://lucene.apache.org/solr/downloads.html 2. 将solr部署到Tomcat中 ...
- 揭秘Node.js深受欢迎的原因
揭秘Node.js深受欢迎的原因 http://www.php100.com/html/dujia/2014/1127/7922.html
- bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】
先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...
- OpenGL 2D模式
// // left top 这里设置的默认是左上角 // void push_view2d(int left, int top, int width, int height) { //glPushA ...
- [C陷阱和缺陷] 第5章 库函数
有关库函数的使用,我们能给出的最好建议是尽量使用系统头文件,当然也可以自己造轮子,随个人喜好.本章将探讨某些常用的库函数,以及编程者在使用它们的过程中可能出错之处. 5.1 返回整数的getc ...
- [SDOI2013]泉
题目描述 作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上x个不同年份时不同泉区的水流指数,这个指数是一个小于. 2^30的非负整数.第i个年份时六个泉区的泉水流量指数分别为 A(i,l),A( ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Train Seats Reservation
You are given a list of train stations, say from the station 11 to the station 100100. The passenger ...
- Snackbar:用它来替换Toast 显示短提示
简介 Snackbar 它是Toast的子类.主要用来提示短暂的提示信息后,然后它自动消失. 它寄生在普通view上,具有一些基本功能. 它寄生在 CoordinatorLayout 时,有以下两个特 ...
- PHP 操作数据库乱码 以及调试
mysql> show create database pxscj;+----------+--------------------------------------------------- ...