uni-app如何进行数据跨页面传输

在登陆页面中的登陆框中获取用户名及学号,点击登录,随后跳转到主页显示上个页面所填写的信息。
希望能给个代码例子,感谢。

跳转到index时,拼到路径上,
index.html?name=小明&id=1

index中onLoad(e){console.log(e.name,e.id)}

运用vuex可以把组件状态数据集中管理起来,将用户名和学号进行存取就行了,每个组件都可以访问这些状态数据

用本地存储 uni.setStorage

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:uni-app解决权限问题,引导用户跳转至设置界面
  • 除此之外, 这篇博客: uni-app 中的路由与页面跳转中的 通过组件进行跳转 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    通过 navigator 组件进行跳转

    属性说明

    属性名类型默认值说明平台差异说明
    urlString 应用内的跳转链接,值为相对路径或绝对路径,如:"../first/first","/pages/first/first",注意不能加 .vue 后缀 
    open-typeStringnavigate跳转方式 
    deltaNumber 当 open-type 为 'navigateBack' 时有效,表示回退的层数 
    animation-typeStringpop-in/out当 open-type 为 navigate、navigateBack 时有效,窗口的显示/关闭动画效果,详见:
    窗口动画
    App
    animation-durationNumber300当 open-type 为 navigate、navigateBack 时有效,窗口显示/关闭动画的持续时间。App
    hover-classStringnavigator-hover指定点击时的样式类,当hover-class="none"时,没有点击态效果 
    hover-stop-propagationBooleanfalse指定是否阻止本节点的祖先节点出现点击态微信小程序
    hover-start-timeNumber50按住后多久出现点击态,单位毫秒 
    hover-stay-timeNumber600手指松开后点击态保留时间,单位毫秒 
    targetStringself在哪个小程序目标上发生跳转,默认当前小程序,值域self/miniProgram微信2.0.7+、百度2.5.2+、QQ

    open-type 有效值

    说明平台差异说明
    navigate对应 uni.navigateTo 的功能 
    redirect对应 uni.redirectTo 的功能 
    switchTab对应 uni.switchTab 的功能 
    reLaunch对应 uni.reLaunch 的功能字节跳动小程序不支持
    navigateBack对应 uni.navigateBack 的功能 
    exit退出小程序,target="miniProgram"时生效微信2.1.0+、百度2.5.2+、QQ1.4.7+

    注意

    • navigator-hover 默认为 {background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;}, <navigator> 的子节点背景色应为透明色。
    • app-nvue 平台只有纯nvue项目(render为native)才支持 <navigator>。非render为native的情况下,nvue暂不支持navigator组件,请使用API跳转。
    • app下退出应用,Android平台可以使用plus.runtime.quit

    示例 查看示例

    <template>
        <view>
            <view class="page-body">
                <view class="btn-area">
                    <navigator url="navigate/navigate?title=navigate" hover-class="navigator-hover">
                        <button type="default">跳转到新页面</button>
                    </navigator>
                    <navigator url="redirect/redirect?title=redirect" open-type="redirect" hover-class="other-navigator-hover">
                        <button type="default">在当前页打开</button>
                    </navigator>
                    <navigator url="/pages/tabBar/extUI/extUI" open-type="switchTab" hover-class="other-navigator-hover">
                        <button type="default">跳转tab页面</button>
                    </navigator>
                </view>
            </view>
        </view>
    </template>
    // navigate.vue页面接受参数
    export default {
        onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
            console.log(option.id); //打印出上个页面传递的参数。
            console.log(option.name); //打印出上个页面传递的参数。
        }
    }

    url有长度限制,太长的字符串会传递失败,可使用窗体通信全局变量,或encodeURIComponent等多种方式解决,如下为encodeURIComponent示例。

    <navigator :url="'/pages/navigate/navigate?item='+ encodeURIComponent(JSON.stringify(item))"></navigator>
    // navigate.vue页面接受参数
    onLoad: function (option) {
        const item = JSON.parse(decodeURIComponent(option.item));
    }

    注意

    • 跳转tabbar页面,必须设置open-type="switchTab"

     

     


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

问题已解决(入门开发,欢迎大家补充新的解决方案)
以下是我的解决方法
https://www.jianshu.com/p/c61e41f30be0?v=1676563548518