组件间传参

  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>
    );
  }
}



### Vue 3 中基于 State 的组件间传参 在 Vue 3 中,状态管理库 Vuex 可用于处理复杂的全局状态管理和组件间的参数传递。通过 Vuex 来集中存储和管理应用的状态,可以方便地实现在不同组件之间共享数据。 #### 安装并配置 Vuex Store 首先,在项目中安装 Vuex 并创建 store 文件夹下的 index.js 文件来初始化 Vuex 店铺: ```javascript // src/store/index.js import { createStore } from 'vuex' export default createStore({ state: { message: 'Hello from Vuex!' }, mutations: { updateMessage(state, newMessage) { state.message = newMessage; } }, actions: {}, modules: {} }) ``` 接着,在 main.js 中引入此店铺实例,并将其挂载到 Vue 实例上: ```javascript // src/main.js import { createApp } from 'vue' import App from './App.vue' import store from './store' // 导入 Vuex 商店 createApp(App).use(store).mount('#app') ``` #### 组件内部访问 State 数据 任何想要读取或修改该消息的组件都可以这样做: - **获取状态**: 使用计算属性 `this.$store.state.message` 或者借助辅助函数如 `mapState()`。 - **提交变更请求 (mutation)** : 调用 `this.$store.commit('updateMessage', newValue)` 方法更新状态中的变量值。 下面是一个简单的例子展示如何在一个按钮点击事件触发时改变存储于 Vuex 中的消息内容[^2]: ```html <!-- ChildComponent.vue --> <template> <div class="child-component"> {{ getMessage }} <button @click="changeMessage">Change Message</button> </div> </template> <script> import { mapState, mapMutations } from 'vuex'; export default { computed: { ...mapState(['message']), getMessage() { return this.message; } }, methods: { ...mapMutations(['updateMessage']), changeMessage() { const newText = prompt("Enter a new text:"); if(newText !== null){ this.updateMessage(newText); } } } } </script> ``` 这样就实现了通过 Vuex 进行组件间的参数传递以及状态同步操作。值得注意的是,虽然这里只展示了基础功能,但在实际开发过程中还可以进一步优化性能、增强安全性等方面的工作[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值