美文网首页js
vue中的prop

vue中的prop

作者: 回不去的那些时光 | 来源:发表于2018-12-05 22:56 被阅读18次

学习vue有一段时间了,也写了一些东西。今天看文档突然看到了一个好玩的东西,那就是prop。prop的作用是父组件中引用了子组件并给子组件加了一个属性,这个属性可以是静态的,可以是动态的,可以是数字,可以是字符串,可以是数组,还可以是对象。接下来就展示改怎么用。

传静态的属性

子组件:

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  props: ["msg"]
}
</script>

父组件:

<template>
  <div id="app">
    <HelloWorld msg="hello"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  }
};
</script>

好了,这样就完成了一个最简单的使用prop接收父元素的值

传动态字符串

子组件

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  props: ["msg"]
}
</script>

父组件

<template>
  <div id="app">
    <HelloWorld :msg="hello"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {
      hello: "hello world"
    }
  }
};
</script>

可以看到,子组件我未做任何修改,只是在父组件做了一些修改

传动态数组

子组件

<template>
  <div>
    <ul>
      <li v-for="item in msg" :key="item">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ["msg"]
}
</script>

父组件

<template>
  <div id="app">
    <HelloWorld :msg="hello"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {
      hello: ["aa", "bb", "cc"]
    }
  }
};
</script>

传动态对象

子组件

<template>
  <div>
    <h1>{{ msg.id }}</h1>
    <h2>{{ msg.name }}</h2>
  </div>
</template>

<script>
export default {
  props: ["msg"]
}
</script>

父组件

<template>
  <div id="app">
    <HelloWorld :msg="hello"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {
      hello: {
        id: 1,
        name: "hello dj"
      }
    }
  }
};
</script>

对prop的状态进行验证

prop的状态可以为

  • String
  • Number
  • Boolean
  • Array
  • Object
  • Date
  • Function
  • Symbol
    下面进行演示
    子组件
<template>
  <div>
    <h1>{{ num }}</h1>
    <h1>{{ msg }}</h1>
    <h1>{{ obj.id }}</h1>
    <h2>{{ obj.name }}</h2>
    <ul>
      <li v-for="item in arrs" :key="item">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    num: {
      type: Number
    },
    msg: {
      type: String
    },
    arrs: {
      type: Array
    },
    obj: {
      type: Object
    }
  }
}
</script>

父组件

<template>
  <div id="app">
    <HelloWorld :num="num" :msg="hello" :arrs="arr" :obj="post"/>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  components: {
    HelloWorld
  },
  data() {
    return {
      num: 10,
      hello: "hello world",
      arr: ["aa","bb","cc"],
      post: {
        id: 1,
        name: "hello dj"
      }
    }
  }
};
</script>

上面演示了Number,String,Array,object的用法

相关文章

  • 对vue的props实现双向数据流

    1、 使用Prop需要遵守的规定 vue官网中规定,vue中的Prop是单向数据流 在vue中,组件中的Prop是...

  • vue中的prop

    学习vue有一段时间了,也写了一些东西。今天看文档突然看到了一个好玩的东西,那就是prop。prop的作用是父组件...

  • Vue Prop属性功能与用法实例

    这篇文章主要介绍了Vue Prop属性功能与用法,结合实例形式较为详细的分析了vue.js中Prop属性的功能、原...

  • elementUI 参数传递类型错误 2020-04-30

    [Vue warn]: Invalid prop: type check failed for prop "min...

  • Vue中的prop属性

    用来接收父组件传给子组件的数据,可以在组件上注册一些自定义特性,当一个值传递给一个prop特性的时候,它就变成那个...

  • vue组件通信(父子)

    在 Vue 中,父子组件的关系可以总结为 prop 向下传递,事件向上传递。父组件通过 prop 给子组件下发数据...

  • vue中爆错Error in render: "TypeErro

    在vue中爆错的解决办法Error in render: "TypeError: Cannot read prop...

  • 深入浅析Vue中的Prop

    Prop 基本用法 Prop的基本用法很简单,只需要在子组件的Vue实例中定义该属性并把值设为目标属性的数组即可 ...

  • vue~ prop

  • vue prop

    问题描述在vue项目中,父组件通过prop给子组件传值时,如果prop值是从服务器端获取,则父组件可能会传给子组件...

网友评论

    本文标题:vue中的prop

    本文链接:https://www.haomeiwen.com/subject/sxugcqtx.html