vue的props和$attrs
过去我们在vue的父子组件传值的时候,我们先需要的子组件上用props注册一些属性:
<template>
<div>
props:{{name}},{{age}} 或者 {{$props['name']}},{{$props['age']}}
</div>
</template> export default{
props: ['name','age']
}
然后父组件调用的时候当属性来传值
<child name="rick" :age="18"></child>
如果我们给child传props没有注册的属性,我们就要用$attrs来取了
<child name="rick" :age="18" gender="male"></child>
child:
<template>
<div>
props:{{name}},{{age}} 或者 {{$props['name']}},{{$props['age']}}
<br>
attrs: {{$attrs['gender']}} 在$attrs里面只会有props没有注册的属性
</div>
</template> export default{
props: ['name','age']
}
当然这个$attrs是vue2.4才推出的,为了简化父组件和孙组件的传值:
父组件 template(假设gender属性没有被props注册):
<child1 gender="male"></child1>
child1 template(v-bind=”$attrs”,这是v-bind唯一可以直接跟等号的特殊写法):
<child2 v-bind=”$attrs”></child2>
在child2里面,就可以直接用props注册gender,来直接获取来自“祖父组件”的gender值了(当然,不注册也是可以用$attrs来取值的)
vue的props和$attrs的更多相关文章
- vue组件通信(props,$emit,$attrs,$listeners)
		朝颜陌 vue基础----组件通信(props,$emit,$attrs,$listeners) 一.父传子,子传孙 1. props 1>在父组件中通过子组件自定义的标签属性来传递数据. ... 
- vue基础----组件通信(props,$emit,$attrs,$listeners)
		一.父传子,子传孙 1. props 1>在父组件中通过子组件自定义的标签属性来传递数据. 2>在子组件中通过props声明希望用到的数据 <body> <div id= ... 
- Vue - 组件通信之$attrs、$listeners
		前言 vue通信手段有很多种,props/emit.vuex.event bus.provide/inject 等.还有一种通信方式,那就是 $attrs 和 $listeners,之前早就听说这两个 ... 
- vue & components & props & methods & callback
		vue & components & props & methods & callback demo solution 1 & props & data ... 
- [转]Vue中用props给data赋初始值遇到的问题解决
		原文地址:https://segmentfault.com/a/1190000017149162 2018-11-28更:文章发布后因为存在理解错误,经@Kim09AI同学提醒后做了调整,在此深表感谢 ... 
- Vue中用props给data赋初始值遇到的问题解决
		Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14 作者:yuyongyu 我要评论 这篇文章主要介绍了Vue中用props给dat ... 
- Vue computed props pass params
		Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ... 
- vue & watch props
		vue & watch props bug OK watch: { // props // chatObj: () => { // // bug // log(`this.chatObj ... 
- vue & modal props & form data update bug
		vue & modal props & form data update bug OK <div> <BindModal :dialogBindVisible=&qu ... 
随机推荐
- Team Member Introduction and Division of Work
			Team leader Name:宋天舒 Student Number:12061166 Interested In: Information safety. Responsible For: Des ... 
- 封装,策略,Asp换脸
			封装.策略 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ... 
- mysql非安装包安装教程
			设置mysql的环境变量 本人设置安装的路径是:E:\WebApplication\webMySQL\mysql-5.7.13-winx64 我的电脑 ---> 高级系统配置 ---> 环 ... 
- DPDK L3fwd 源码阅读
			代码部分 整个L3fwd有三千多行代码,但总体思想就是在L2fwd的基础上,增加网络层的根据 IP 地址进行路由查找的内容. main.c 文件 int main(int argc, char **a ... 
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
			1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ... 
- bash简介1
			bash脚本语言文件格式 第一行#!/bin/bash :定义bash脚本解释器 注释信息:# 代码注释: 缩进,适度添加空白行 bash中的变量介绍: 局部变量 本地变量 环境变量 位置参数 ... 
- CASE (Transact-SQL)
			A. 使用带有 CASE 简单表达式的 SELECT 语句Using a SELECT statement with a simple CASE expression在 SELECT 语句中,CASE ... 
- web_custom_request和web_submit_data
			网络上很多说明这2个函数区别的文章,我就从其他摘抄了内容,其中区别自己查看附录,我主要说明2点 (1)用web_custom_request提交请求如果是json,则会会使用关键字符{},但是{},是 ... 
- 斑马条码打印机GK888T打印标签是间隔的 ,不是连续的
			有间断的标签纸和连续的标签纸是两种不同的纸张类型, 打印机的标签感应器需要工作在不同的模式来跟踪感应它们. 打印机正确感应纸张才不会红灯闪烁,打印的内容才按文件设计打印到标签的对应位置上. 所以要在驱 ... 
- 【设计模式】—— 原型模式Prototype
			前言:[模式总览]——————————by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ... 
