父组件往子组件传值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <!--父组件通过调用v-bind把自己的nameList传递给子组件-->
    <my-button v-for="(item,index) in nameList"
               v-bind:key="index"
               v-bind:name="item"
    ></my-button>
</div>
<script src="vue.js"></script>
<script>

    //注册全局组件  子组件
    Vue.component("my-button",{
        template:`<h1>{{ this.name }}</h1>`,
        props:["name"],  //告诉别人这里需要传入一个name参数
        data(){
            return{ }
        }
    });

    // 根实例  父组件
    let app = new Vue({
        el:"#app",
        data:{
            nameList:["alex","wusir","mjj"]
        }
    })
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/kenD/p/10230961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 React 中,可以通过 props 将数据从组件传递给子组件。如果组件的数据发生变化,可以通过 setState 方法更新组件的状态,并将新的状态传递给子组件。子组件可以通过 componentWillReceiveProps 方法监听组件传递过来的新的 props 值,并根据新的值进行相应的变化。 以下是一个示例代码: ```javascript // 组件 class ParentComponent extends React.Component { constructor(props) { super(props); this.state = { value: '' }; this.handleChange = this.handleChange.bind(this); } handleChange(e) { this.setState({ value: e.target.value }); } render() { return ( <div> <input value={this.state.value} onChange={this.handleChange} /> <ChildComponent inputValue={this.state.value} /> </div> ); } } // 子组件 class ChildComponent extends React.Component { constructor(props) { super(props); this.state = { outputValue: '' }; } componentWillReceiveProps(nextProps) { if (nextProps.inputValue !== this.props.inputValue) { this.setState({ outputValue: nextProps.inputValue }); } } render() { return <div>{this.state.outputValue}</div>; } } ``` 在这个示例中,组件中的 input 组件的值发生变化时,会触发 handleChange 方法,更新组件的 value 状态。同时,也会将新的 value 值传递给子组件 ChildComponent。当子组件接收到新的 inputValue 值时,会触发 componentWillReceiveProps 方法,更新子组件的 outputValue 状态。因此,当组件的 value 值发生变化时,子组件的内容也会随之变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值