umijs4中可以两个组件connect一个dva吗
时间: 2023-11-28 22:47:23 浏览: 73
是的,你可以使用 `@connect` 装饰器将两个组件连接到一个 dva model 中。 在 `umi` 中,你也可以使用 `connect` 函数将组件连接到 dva model 中。
具体来说,你可以在两个组件中都使用 `@connect` 装饰器,并将它们连接到同一个 dva model 中。 例如:
```jsx
// ComponentA.js
import { connect } from 'umi';
import React from 'react';
@connect(({ myModel }) => ({
data: myModel.data,
}))
class ComponentA extends React.Component {
// ...
}
export default ComponentA;
// ComponentB.js
import { connect } from 'umi';
import React from 'react';
@connect(({ myModel }) => ({
data: myModel.data,
}))
class ComponentB extends React.Component {
// ...
}
export default ComponentB;
```
在上面的示例中,我们将 `ComponentA` 和 `ComponentB` 连接到 `myModel` model 中。 它们都可以访问 `myModel` 中的 `data` 属性。
如果你不想使用装饰器,你也可以使用 `connect` 函数将组件连接到 dva model 中。 例如:
```jsx
// ComponentA.js
import { connect } from 'umi';
import React from 'react';
class ComponentA extends React.Component {
// ...
}
export default connect(({ myModel }) => ({
data: myModel.data,
}))(ComponentA);
// ComponentB.js
import { connect } from 'umi';
import React from 'react';
class ComponentB extends React.Component {
// ...
}
export default connect(({ myModel }) => ({
data: myModel.data,
}))(ComponentB);
```
这个示例中,我们使用 `connect` 函数将 `ComponentA` 和 `ComponentB` 连接到 `myModel` model 中。 它们都可以访问 `myModel` 中的 `data` 属性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)