首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过路由器链接Vuejs传递对象

通过路由器链接Vuejs传递对象
EN

Stack Overflow用户
提问于 2018-07-07 11:08:33
回答 2查看 9.6K关注 0票数 2

我有一个双组分总线列表和总线配置文件。总线列表包含几个名为总线的图标。当我单击总线图标时,它必须从请求的api中传递对象项。item对象如下所示:

项目:{ "id":1,"bus_number":"xx-xx-xxx","bus_color":"black“}

这是我的代码:

代码语言:javascript
复制
            In my Bus list.vue file
            <template slot="bus" slot-scope="data">
                              <span class="bus" v-for="bus in buss">
                                <router-link :to="bus.url + data.item">
                                  <a><i :key="data.item.id" ></i>
                                    <a><i v-bind:class="bus.icon"></i></a>
                                  </a>
                                  </router-link>
                                  </span>
            </template>

            In java script part
            data () {
            return {
            buss: [
                      {url: '/xxxx/xxxxxx/xxxxxx/', icon: 'fa fa-bus fa-2x text-success'}
                    ],
            }, 
             methods: {
              getBusInfo: function () {
                axiosWithOutAuth({
                  method: 'post',
                  url: 'xxx/xxxx/xxxx',
                  headers: {Authorization: 'JWT ' + localStorage.getItem('usertoken')}
                }).then(response => {
                  console.log(response.data.results)
                  this.items = response.data.results


                }).catch(function (error) {
                  console.log(error)
                })
              },

            }

          In my component file where i named as index.js
        {
                      path: 'bus-profile/:item',
                      name: 'agency-bus-profile',
                      component: TravelAgencyBusProfile,
                      props: true
        }

        In my bus profile.vue file...
        I am showing how i tried myself  to access object
         export default {
            name: 'agency-bus-profile',
            props: [item],

          }
        </script>

请注意这里的语法错误对我来说并不重要。这里我只想知道如何使用路由器链接和道具直接将对象从一个组件传递到另一个组件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-07 20:56:32

您可以使用query向路由传递其他数据:

代码语言:javascript
复制
<router-link :to="{
    path: bus.url + data.item,
    query: {
        item: { "id": 1, "bus_number": "xx-xx-xxx", "bus_color": "black" }
    }
}">

然后您可以使用this.$route.query访问它们。

票数 4
EN

Stack Overflow用户

发布于 2020-06-06 21:42:41

这很简单,请查看下面的示例

代码语言:javascript
复制
<router-link :to="{
    path: '/path_example',
    query: {  // or if you want to use params: { ley1: value1, ke2: value2, ...}
        item: { key: value_1, ke2: value2, ... }
    }
}">

若要从另一个视图访问它,请使用

代码语言:javascript
复制
const item = this.$router.params.item
const item = this.$router.query.item

搜索"vue发送对象路由器链接“,然后查看文档或从军校观看视频。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51222587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档