- 59
- 0
点击传递不同的文章id,但是文章不值一页,我的思路是,先传id给父组件,然后通过父组件给子组件传参,传递给每个子组件ID,这样每个子组件都是这个id下的文章,但是现在出现了个问题,就是传递id之后,父组件能够正常接收id,但是一刷新.父组件的id就会消失,之前传文章就一个页面,id不会消失,求大大解决,只能用每个路由带着参数互相传递吗?不能直接父组件拿到ID,直接出传递给每个子组件吗?
这是路由配置.通过name和params传参.普通只有一个子组件刷新id不会消失
path: '/information/:id', //信息披露
name:'information',
component: information,
children: [
{ path: '/', name: 'informantion_catalogue', component: informantion_catalogue },
{ path: 'information_growth', name: 'information_growth', component: information_growth },
{ path: 'information_invest', name: 'information_invest', component: information_invest },
{ path: 'informantion_LenderAnalysis', name: 'informantion_LenderAnalysis', component: informantion_LenderAnalysis },
{ path: 'informantion_events', name: 'informantion_events', component: informantion_events },
{ path: 'informantion_BorrowingUser', name: 'informantion_BorrowingUser', component: informantion_BorrowingUser },
{ path: 'information_proportion', name: 'information_proportion', component: information_proportion },
{ path: 'information_lifeLoan', name: 'information_lifeLoan', component: information_lifeLoan },
{ path: 'informantion_area', name: 'informantion_area', component: informantion_area },
{ path: 'informantion_targetProject', name: 'informantion_targetProject', component: informantion_targetProject },
{ path: 'informantion_endpage', name: 'informantion_endpage', component: informantion_endpage },
]
}
id:id 绑定给每个子组件 ,但是刷新就会消失.求大大们解答.这种怎么解决比较好
<template>
<div id="information">
<transition :name="transformName">
<keep-alive>
<router-view class="child-view" @arrow="sayToInformation" mode :id="id" ></router-view>
</keep-alive>
</transition>
</div> - 共 0 条
- 全部回答
-
队友在哪 普通会员 1楼
在React中,父组件将参数传递给子组件的方式通常是通过props。如果你遇到刷新父组件时参数消失的问题,这可能是因为这些参数没有被持久化存储或者没有在合适的生命周期方法中进行初始化。
例如,如果父组件是从URL查询参数、localStorage、API请求等获取的动态数据,你需要确保在每次组件重新渲染(如componentDidMount, componentDidUpdate或者useEffect Hook)时都重新获取并设置这些参数。
以下是一个基本示例:
```jsx class ParentComponent extends React.Component { constructor(props) { super(props); this.state = { params: null // 假设这是你要传递的参数 }; }
componentDidMount() { // 在这里从某个地方获取参数,比如API请求 fetchSomeData() .then(data => this.setState({ params: data })); }
render() { return (
{this.state.params &&); } }} function ChildComponent({ params }) { // 使用传递进来的params // ... } ```
对于函数组件,你可能会使用
useEffectHook来处理这个问题:```jsx import React, { useState, useEffect } from 'react';
function ParentComponent() { const [params, setParams] = useState(null);
useEffect(() => { async function fetchData() { const data = await fetchSomeData(); setParams(data); }
fetchData();}, []); // 确保在组件挂载时只执行一次
return (
{params &&); }} function ChildComponent({ params }) { // 使用传递进来的params // ... } ``` 这样,即使父组件刷新,只要保证在恰当的时机重新获取并设置参数,就可以避免参数消失的问题。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

