组件间传参

  1. 父组件传子组件
class Son extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'son'
    };
  }

  clickBtn() {
    this.props.callBack(this.state.name);
  }

  render() {
    return (
      <div>
        <div>name:{this.props.name}</div>
        <button onClick={this.clickBtn.bind(this)}>改变名称</button>
      </div>
    );
  }
}

class Father extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'father'
    };
  }

  callBack(name) {
    this.setState({ name: name });
  }

  render() {
    return (
      <div>
        <Son name={this.state.name} callBack={this.callBack.bind(this)} />
      </div>
    );
  }
}

2.子组件传父组件

class Son extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'son',
      msg: 'Son Message'
    };
    this.sendMsg = this.sendMsg.bind(this);
  }

  clickBtn() {
    this.props.callBack(this.state.name);
  }
  // 在子组件中通过this.props获取修改父组件的方法
  sendMsg() {
    this.props.msgFromChild(this.state.msg);
  }

  render() {
    return (
      <div>
        <div>name:{this.props.name}</div>
        <button onClick={this.clickBtn.bind(this)}>改变名称</button>
        <br />
        <button onClick={this.sendMsg}>发送信息</button>
      </div>
    );
  }
}

class Father extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'father',
      msg: ''
    };
  }

  callBack(name) {
    this.setState({ name: name });
  }
  // 通过子组件中传递过来的msg对父组件state的msg进行修改
  msgFromChild(msg) {
    this.setState({
      msg: msg
    });
  }

  render() {
    return (
      <div>
        <div>Msg: {this.state.msg}</div>
        <Son
          name={this.state.name}
          callBack={this.callBack.bind(this)}
          msgFromChild={this.msgFromChild.bind(this)}
        />
      </div>
    );
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,组件传参有多种方式。下面是几种常见的传参方式: 1. Props(属性):可以通过在父组件中使用v-bind指令,将数据作为props传递给子组件。子组件可以在props选项中定义接收的属性,并在模板中使用这些属性。 父组件模板中的示例代码: ``` <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { message: 'Hello from parent' } } } </script> ``` 子组件中的示例代码: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: { message: { type: String, required: true } } } </script> ``` 2. $emit(事件):可以通过在子组件中使用$emit方法触发自定义事件,然后在父组件中监听此事件并响应。 子组件中的示例代码: ``` <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('message-sent', 'Hello from child'); } } } </script> ``` 父组件模板中的示例代码: ``` <template> <div> <child-component @message-sent="handleMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleMessage(message) { console.log(message); } } } </script> ``` 这些是常见的组件传参方式,你可以根据具体需求选择适合的方式。如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值