Vue- 组件与props属性的用法
来源:互联网 发布:淘宝唱片店正版 编辑:程序博客网 时间:2024/05/19 22:02
在 Vue 里,一个组件实质上是一个拥有预定义选项的一个 Vue 实例:(单独测试的时候记得new Vue()渲染)
// Define a new component called todo-itemVue.component('todo-item', {template: '<li>This is a todo</li>'})
现在你可以在另一个组件模板中写入它:
<ol><!-- Create an instance of the todo-item component --><todo-item></todo-item></ol>
但是这样会为每个 todo 渲染同样的文本,这看起来并不是很酷。我们应该将数据从父作用域传到子组件。让我们来修改一下组件的定义,使得它能够接受一个 prop
字段(属性):
props 把数据传给子组件!!!
“prop” 是组件数据的一个字段,期望从父作用域传下来。子组件需要显式地用 props 选项 声明 props:
Vue.component('todo-item', {// The todo-item component now accepts a// "prop", which is like a custom attribute.// This prop is called todo.props: ['todo'],template: '<li>{{ todo.text }}</li>'})
现在,我们可以使用 v-bind
指令将 todo 传到每一个重复的组件中:
<div id="app-7"><ol><!-- Now we provide each todo-item with the todo object --><!-- it's representing, so that its content can be dynamic --><todo-item v-for="item in groceryList" v-bind:todo="item"></todo-item></ol></div>
Vue.component('todo-item', {props: ['todo'],template: '<li>{{ todo.text }}</li>'})var app7 = new Vue({el: '#app-7',data: {groceryList: [{ text: 'Vegetables' },{ text: 'Cheese' },{ text: 'Whatever else humans are supposed to eat' }]}})
- Vegetables
- Cheese
- Whatever else humans are supposed to eat
0 0
- Vue- 组件与props属性的用法
- Vue系列: 如何通过组件的属性props设置样式
- Vue-组件props属性再回顾
- Vue的Props属性概述
- vue子组件获取父组件的内容(props属性)
- vue中component组件的props使用
- Vue组件选项props
- Vue之props属性
- Vue props用法总结
- React 组件间通信-props的用法
- 关于Vue.js的组件化,使用props传递数据
- vue组件3-父子组件props传参
- 父子组件通信-props属性
- React Native组件的生命周期及属性props
- vue组件4-props传参2
- vue 分页组件及props传参
- vue组件学习6(props传参)
- vue的props实现父组件变化子组件一起变化
- SpringMVC @RequestBody接收Json对象字符串
- Android 代码混淆异常 transformClassesAndResourcesWithProguardForRelease FAILED
- Java Cache系列之Guava Cache
- leetcode 144. Binary Tree Preorder Traversal
- ACM 背包问题 Piggy-Bank
- Vue- 组件与props属性的用法
- 使用Google Guava来编写优雅的代码一Guava简介
- PHP基础语法
- elasticsearch三个重要的优化
- 一篇文章告诉你什么是量化的统计套利
- tomcat Could not load the Tomcat server configuration at \Servers\Tomcat v8.0 Server at localhost-co
- Oracle bulk into clause
- Google Guava 类库简介
- pt-online-schema-change在线修改MYSQL表结构