跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 卖家故事 > 初学Vue(全家桶)-第n天(vue2):关于v-bind=“$attrs“、v-bind=“$props“ 和v-on=“$listeners“的使用

初学Vue(全家桶)-第n天(vue2):关于v-bind=“$attrs“、v-bind=“$props“ 和v-on=“$listeners“的使用

时间:2024-04-21 14:10:29 来源:网络cs 作者:亙句 栏目:卖家故事 阅读:

标签: 使用 
阅读本书更多章节>>>>

初学vue

简介

1、v-bind="$props": 可以将父组件的所有props下发给它的子组件,子组件需要在其props:{} 中定义要接受的props。

2、v-bind="$attrs": 将调用组件时的组件标签上绑定的非props的属性(class和style除外)向下传递。在子组件中应当添加inheritAttrs: false(避免父作用域的不被认作props的特性绑定应用在子组件的根元素上)。

3、v-on="$listeners":通过this.emit(‘事件名’,数据),将底层级的信息往高层级传递。

1、v-bind=“$props”

有如下层级关系:
父组件TheOuter想向子组件TheInner传值

<App><TheOuter> <TheMiddle> <TheInner></TheInner></TheMiddle></TheOuter></App>
App.vue
<template>  <div id="app">  <!--往下传值-->    <TheOuter :id="1" :name="'jack'" :age="21"></TheOuter>  </div></template><script>import TheOuter from "./components/TheOuter.vue";export default {  name: "App",  components: {    TheOuter,  },};</script>
TheOuter.vue
<template>  <div>  <!--继续往下传值-->    <!--<TheMiddle :id="id" :name="'name'" :age="age"></TheMiddle>--> <!-- 不使用v-bind="$props"时这么传值 -->    <TheMiddle v-bind="$props" gender="man"></TheMiddle>  </div></template><script>import TheMiddle from "./TheMiddle.vue";export default {  name: "TheOuter",  components: { TheMiddle },  props:['id','name','age'], // 接收prop值  data() {    return {};  },  mounted() {    console.log(this.id, this.name, this.age,"这是TheOuter组件");  },};</script>
TheMiddle.vue
<template>  <div>  <!--继续往下传值-->  <!--<TheInner :id="id" :name="'name'" :age="age"></TheInner>--> <!-- 不使用v-bind="$props"时这么传值 -->    <TheInner v-bind="$props"></TheInner>  </div></template><script>import TheInner from "./TheInner.vue";export default {  name: "TheMiddle",  components: { TheInner },  props:['id','name','age'], // 接收prop值  data() {    return {};  },  mounted(){    console.log(this.id, this.name, this.age,"这是TheMiddle组件");  }};</script>
TheInner.vue
<template>  <div>    <div>hello啊</div>  </div></template><script>export default {  name: "TheInner",  props: ["id", "name", "age"], // 接收prop值  data() {    return {};  },  mounted() {    // console.log(this.$props["name"]);    console.log(this.id, this.name, this.age, "这是TheInner组件");  },};</script>

2、v-bind=“$attrs”

例如,有如下层级关系:
父组件TheOuter想向子组件TheInner传值

<App><TheOuter> <TheMiddle> <TheInner></TheInner></TheMiddle></TheOuter></App>
App.vue
<template>  <div id="app">    <TheOuter id="1" name="jack" age="21"></TheOuter>  </div></template>
TheOuter.vue
<template>  <div>    <TheMiddle v-bind="$attrs" gender="man"></TheMiddle>  </div></template>// ----------------------------------mounted(){  // console.log(this.$attrs['id']); // 1  // console.log(this.$attrs['name']); // jack  // console.log(this.$attrs['age']); // 21}
TheMiddle.vue
<template>  <div>    <TheInner v-bind="$attrs"></TheInner>  </div></template>// ----------------------------------mounted(){  // prop中变量的使用  // console.log(this.$attrs['id']); // 1  // console.log(this.$attrs['name']); // jack  // console.log(this.$attrs['age']); // 21  // console.log(this.$attrs['gender']); // man}
TheInner.vue
<template>  <div>    <div>hello啊</div>  </div></template>// ----------------------------------mounted(){  // prop中变量的使用  // console.log(this.$attrs['id']); // 1  // console.log(this.$attrs['name']); // jack  // console.log(this.$attrs['age']); // 21  // console.log(this.$attrs['gender']); // man}

v-bind="$attrs"的作用是将调用组件时的组件标签上绑定的非props的属性(class和style除外)向下传递
你可以发现,使用了v-bind='$attrs'的组件,都可以使用从上级组件中获取值,并且它的下级组件直接下级组件即便没有用v-bind='$attrs'绑定,同样也可以获取到值。

需要注意的是,可以用props来接收传递过来的值,但不要同时用props配置项和this.\$props['xxx']调用同一个值,因为props优先级比this.$props['xxx']高,会导致this.$props['xxx']的结果为undefined。

3、v-on=“$listeners”

和上面的this.$ attrs['xxx']相反,v-on="$listeners"作用是用于底层级组件向高层级组件传递信息。通过this.$emit()触发事件,来进行信息传递,

例如有父组件TheOuter,子组件TheMiddle孙组件TheInner 三个组件,如果TheInner传递信息给TheMiddle则可直接使用$emit,如果是TheInner向TheOuter传递信息,则就需要TheInner先$emit给TheMiddle,TheMiddle再$emit给TheOuter,这种方式比较繁琐,则此时可以使用v-on="$listeners"来满足当前需求。

具体使用

层级关系如下:

<App><TheOuter> <TheMiddle> <TheInner></TheInner></TheMiddle></TheOuter></App>
App.vue
<template>  <div id="app">    <TheOuter></TheOuter>  </div></template>
TheOuter.vue
<template>  <div>    <!-- $emit触发sendMsg事件,sendMsg事件执行sendMsg()方法,输出从底层接收到的值 -->    <TheMiddle @sendMsg='sendMsg'></TheMiddle>  </div></template>// --------------------------------------  methods:{    sendMsg(msg){      console.log("收到信息了,这里是TheOuter.vue组件",msg);    }  }
TheMiddle.vue
<template>  <div>    <!-- 通过v-on=$listeners 将要触发的事件和数据往上层传递-->    <TheInner v-on="$listeners"></TheInner>  </div></template>// 同样这里也可以调用mounted(){this.$listeners.sendMsg()}
TheInner.vue
<template>  <div>    <button @click="handleClick">hello啊</button>  </div></template>// -----------------------------------  methods:{  // 点击事件触发的函数    handleClick(){      // 调用this.$emit,往上层传递要触发的事件名和数据      this.$emit("sendMsg",'hello啊')      // 或者      // this.$listeners.sendMsg('hello啊')    }  }// ------------------------------------// 或者更简单点写,直接不写方法了<template>  <div>    <button @click="$emit('sendMsg','hello啊')">hello啊</button>  </div></template>
阅读本书更多章节>>>>

本文链接:https://www.kjpai.cn/gushi/2024-04-21/160938.html,文章来源:网络cs,作者:亙句,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

文章评论