work-notes(1):父子组件传值,props/$emit 简单易懂

news/2024/7/10 1:47:46 标签: js, vue

时间:2022-04-11

1、props(父 => 子)用法

在父组件中,引入子组件的标签内绑定;

// 父组件:
<template>
  <div>
 	 // 1、在子组件标签内冒号绑定 
 	 // bind_data 这个名称是传到 自组件 后数据(变量)的名称
 	 // fater_data 是父组件中变量名称
    <children :bind_data:"fater_data"></children>
  </div>
</template>
// 子组件:
<template>
  <div>
  	// 2、展示父组件传过来的数据,这里可以写你自己要处理的形式,我这里直接放上来
 	 {{bind_data}}
  </div>
</template>
<script>
// 1、导入子组件
import children from "@/components/children";
export default {
  data() {
    return {
    };
  },
  // 1、引入父组件的数据
  props: {
	bind_data: {
      likes: "object",
      type: Object,
      required: true,
    },
  },
  methods: {
  }
</script>

父组件(props例子)

// 父组件:
<template>
  <div>
 	 // 2、引用子组件
    <children :bind_data:"fater_data"></children>
  </div>
</template>
<script>
// 1、导入子组件
import children from "@/components/children";
export default {
  components: {
    // 3、注册子组件 children
    children
  },
  data() {
    return {
      fater_data: {name:'xxx', age:'99'} // 看你自己实际数据,这里瞎编的
    };
  },
  methods: {
  }
</script>

子组件(props例子)

// 子组件:
<template>
  <div>
  	// 2、展示父组件传过来的数据,这里可以写你自己要处理的形式,我这里直接放上来
 	 {{bind_data}}
  </div>
</template>
<script>
// 1、导入子组件
import children from "@/components/children";
export default {
  data() {
    return {
    };
  },
  // 1、引入父组件的数据
  props: {
	bind_data: {
      likes: "object",
      type: Object,
      required: true,
    },
  },
  methods: {
  }
</script>

2、$emit (实现方法传值)

函数 写在父组件

父组件($emit例子)

// 父组件:
<template>
  <div>
 	 // 1、在引入子组件的标签 写入函数
 	 // delete-btn	函数在 子组件中的名称
 	 // faterDelete	函数在 父组件中实际名称
    <children :bind_data:"fater_data" @delete-btn:"faterDelete"></children>
  </div>
</template>
<script>
import children from "@/components/children";
export default {
  components: {
    // 3、注册子组件 children
    children
  },
  data() {
    return {
      fater_data: {name:'xxx', age:'99'} // 看你自己实际数据,这里瞎编的
    };
  },
  methods: {
  	// 2、写在父组件中的函数
  	faterDelete(val){
		// 删除操作
	}
  }
</script>

子组件($emit例子)

// 子组件:
<template>
  <div>
  	// 1、子组件中用 emit 方法
 	 <button @click="$emit('delete-btn', bind_data)"></button>
  </div>
</template>
<script>
// 1、导入子组件
import children from "@/components/children";
export default {
  data() {
    return {
    };
  },
  props: {
	bind_data: {
      likes: "object",
      type: Object,
      required: true,
    },
  },
  // 2、这里类似 声明函数名称
  emits: [
  	'delet-btn',
  ]
};
</script>

http://www.niftyadmin.cn/n/1410350.html

相关文章

git 完成自己本地代码后如何提交到公司项目(远程仓库)?

一、理清分支情况 1、远程仓库 和 分支&#xff1a; 远程仓库&#xff1a;比如说&#xff0c;github、gitlab、gitee 这些存放在‘云端’的项目代码。在远程仓库上的分支称为&#xff1a;远程仓库分支&#xff1b;远程仓库 主分支&#xff1a;远程仓库上&#xff0c;默认在创…

work-notes(2):导航守卫

时间&#xff1a;2022-04-08 代码 router.beforeResolve((to, from , next)>{var tokens Cookies.get(ZIWU_NAVI)if (to.path ! /login && tokens undefined){// 判断是否在登录界面并且tokens不存在next(/login)} else if (to.path /login && tokens …

分布式缓存系统热点数据

一、背景 分布式缓存一般被定义为一个数据集合&#xff0c;它将数据分布&#xff08;或分区&#xff09;于任意数目的集群节点上。集群中的一个具体节点负责缓存中的一部分数据&#xff0c;整体对外提供统一的访问接口 Amazon 于 2007 年提出的一种改进的一致性哈希算法 [4]。…

work-notes(3):前端新项目入手,git拉项目,从头到尾操作理清思路

时间&#xff1a;2022-04-11 文章目录声明开头1、在远程仓库上&#xff0c;创建自己的分支2、clone 克隆项目3、猜你遇到问题4、基于 1、2 步骤本地已有联通项目5、创建本地分支6、拉取代码7、暂存代码8、提交代码9、同步10、可以在远程仓库查看声明 因为对clone、拉取、提交、…

work-notes(4):在.js文件中插入空格,如何插入(拼接)空格?插入多个空格?

时间&#xff1a;2022-04-12 文章目录描述1、在 .js 文件里面1.1 方式11.2 方式22、在 .html 里面描述 今天想在字符串内拼接 空格 老出不来。 主要是我想用 多个空格 &#xff0c;尝试了&#xff1a; ① ‘ ’ (里面多个空格不行)&#xff1b; ② ’ ’ ’ ’ &#xff0…

work-notes(5):v-for报错,[vue/require-v-for-key] Elements in iteration expect to have ‘v-bind:key‘

时间&#xff1a;2022-04-14 文章目录报错情况报错原因解决办法2、如果报错Expected v-bind:key directive to use the variables which are defined by the v-for directive.报错原因解决办法报错情况 报错原因 迭代中的[vue/require-v-for-key]元素希望有“v-bind:key”指令…

work-notes(6):vue中 v-for,class 和 style 中绑定带入变量的写法,如何在style中加入变量?

时间&#xff1a;2022-04-14 更新&#xff1a;2022-04-18 文章目录原因解释:class 写法1、三元运算2、变量3、对象4、数组:style 写法1、变量2、对象3、三元运算原因解释 在 v-for 写入到 style 或 class&#xff0c;特别是绑定的 :style、:class 就不生效 :class 写法 1、…

work-notes(7):如何让元素从中心放大?如何让图片从中心放大?

时间&#xff1a;2022-04-18 文章目录原因解释讲解实现代码原因解释 之前一直在纠结如何让背景图片从中心放大&#xff0c;好像 background-size 做不到这样的效果&#xff1b; 最后找到了一个最简单的方法&#xff0c;一个 CSS 属性就能解决&#xff1b; 讲解 其实就是通过…