路由参数传递

路由参数传递

参数传递

<template>
  <div id="app">
    <div style="display: flex;flex-direction: row;height: 100%;width: 100%;">
      <div style="height: 100%;background-color: antiquewhite;flex-grow: 1">
        <ul style="padding: 0px">
          <router-link tag="li" replace to="/home" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">首页</router-link>
          <router-link tag="li" replace to="/about" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">关于</router-link>
          <li style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">
            <button @click="btn1Click">按钮1</button>
          </li>
          <li style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">
            <button @click="btn2Click">按钮2</button>
          </li>
          <router-link tag="li" replace :to="'/dynamic/'+userId" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">动态路由</router-link>
          <router-link tag="li" replace to="/nested" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由</router-link>
          <li>
            <ul>
              <router-link tag="li" replace to="/nested/nested1" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由1</router-link>
              <router-link tag="li" replace to="/nested/nested2" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由2</router-link>
            </ul>
          </li>
          <router-link tag="li" replace :to="params" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">传递参数</router-link>

        </ul>
      </div>
      <div style="height: 100%;background-color: aqua;flex-grow:6;display: flex;flex-direction: column">
        <div style="background-color: azure;flex-grow: 1">
          我是面包屑
        </div>
        <div style="background-color: cornsilk;flex-grow: 15">
          <router-view></router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'App',
  data(){
    return {
      userId:"Icez",
      params: {
        path: '/transfer',
        query: {
          name: 'icez',
          like: '会android、会PHP(YII)、会VUE、会Flutter、会Electron、会Cocos'
        }
      }
    }
  },
  components: {
  },
  methods: {
    btn1Click(){
      //退入栈中component
      // this.$router.push('/btn1Component')
      //替换当前栈中的后进来的component
      this.$router.replace('/btn1Component')
    },
    btn2Click(){
      //退入栈中component
      // this.$router.push('/btn2Component')
      //替换当前栈中的后进来的component
      this.$router.replace('/btn2Component')
    }
  }
}
</script>

<style>
#app,body,html{
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin: 0px;
  padding: 0px;
  height: 100%;
  width: 100%;
}
.active{
  color: red;
}
</style>

按钮传递参数

<template>
  <div id="app">
    <div style="display: flex;flex-direction: row;height: 100%;width: 100%;">
      <div style="height: 100%;background-color: antiquewhite;flex-grow: 1">
        <ul style="padding: 0px">
          <router-link tag="li" replace to="/home" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">首页</router-link>
          <router-link tag="li" replace to="/about" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">关于</router-link>
          <li style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">
            <button @click="btn1Click">按钮1</button>
          </li>
          <li style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">
            <button @click="btn2Click">按钮2</button>
          </li>
          <router-link tag="li" replace :to="'/dynamic/'+userId" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">动态路由</router-link>
          <router-link tag="li" replace to="/nested" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由</router-link>
          <li>
            <ul>
              <router-link tag="li" replace to="/nested/nested1" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由1</router-link>
              <router-link tag="li" replace to="/nested/nested2" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">嵌套路由2</router-link>
            </ul>
          </li>
          <router-link tag="li" replace :to="params" style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">传递参数</router-link>
          <li style="height: 50px;list-style-type: none;width: 100%;display: flex;align-items: center;justify-content:center">
            <button @click="transferClick">传递参数2</button>
          </li>
        </ul>
      </div>
      <div style="height: 100%;background-color: aqua;flex-grow:6;display: flex;flex-direction: column">
        <div style="background-color: azure;flex-grow: 1">
          我是面包屑
        </div>
        <div style="background-color: cornsilk;flex-grow: 15">
          <router-view></router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'App',
  data(){
    return {
      userId:"Icez",
      params: {
        path: '/transfer',
        query: {
          name: 'icez',
          like: '会android、会PHP(YII)、会VUE、会Flutter、会Electron、会Cocos'
        }
      },
      params2: {
        path: '/transfer2',
        query: {
          name: 'icez',
          like: '会android、会PHP(YII)、会VUE、会Flutter、会Electron、会Cocos'
        }
      }
    }
  },
  components: {
  },
  methods: {
    btn1Click(){
      //退入栈中component
      // this.$router.push('/btn1Component')
      //替换当前栈中的后进来的component
      this.$router.replace('/btn1Component')
    },
    btn2Click(){
      //退入栈中component
      // this.$router.push('/btn2Component')
      //替换当前栈中的后进来的component
      this.$router.replace('/btn2Component')
    },
    transferClick(){
      this.$router.push(this.params2)
    }
  }
}
</script>

<style>
#app,body,html{
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin: 0px;
  padding: 0px;
  height: 100%;
  width: 100%;
}
.active{
  color: red;
}
</style>

接受参数

<template>
  <div>
    传递参数: {{userInfo}}
  </div>
</template>

<script>
export default {
  name: "Transfer",
  data(){
    return {}
  },
  computed: {
    userInfo(){
      return this.$route.query.name + '   ' + this.$route.query.like
    }
  }
}
</script>

<style scoped>

</style>
效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 UniApp 中,可以使用路由参数传递来在页面之间传递据。UniApp提供了多种传递参的方式,下面列举了几种常用的方法: 1. Query 参数传递: 在 `uni.navigateTo` 或 `uni.redirectTo` 方法中,可以通过 `url` 参携带 Query 参来传递据。示例代码如下: ```javascript uni.navigateTo({ url: '/pages/detail?id=1&name=example' }); ``` 在目标页面中,可以通过 `this.$route.query` 或 `uni.getStorageSync` 方法来获取 Query 参的值。 2. URL 传参: 在 `uni.navigateTo` 或 `uni.redirectTo` 方法中,可以通过在 URL 中拼接参来传递据。示例代码如下: ```javascript uni.navigateTo({ url: '/pages/detail/1' }); ``` 在目标页面的 `pages.json` 中配置路由时,可以使用动态路由来匹配 URL 参。例如: ```json { "path": "/pages/detail/:id", "style": { "navigationBarTitleText": "详情页" } } ``` 在目标页面中,可以通过 `this.$route.params` 或 `uni.getStorageSync` 方法来获取 URL 参的值。 3. Storage 传参: 使用 `uni.setStorageSync` 方法将据存储在本地缓存中,然后在目标页面中使用 `uni.getStorageSync` 方法来获取存储的据。示例代码如下: 在当前页面: ```javascript uni.setStorageSync('data', { id: 1, name: 'example' }); uni.navigateTo({ url: '/pages/detail' }); ``` 在目标页面: ```javascript const data = uni.getStorageSync('data'); console.log(data); ``` 以上是几种常用的 UniApp 路由参数传递方式。根据实际需求,选择合适的方式来传递据。在目标页面中,可以通过 `this.$route.query`、`this.$route.params` 或 `uni.getStorageSync` 方法来获取传递的参值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值