简体   繁体   English

访问b表槽中的父组件scope

[英]Access parent component scope in b-table slot

Im using a v-slot in a <b-table> so I can create a link.我在<b-table>中使用了一个v-slot ,所以我可以创建一个链接。

The first part of the link contains data from the datasource.链接的第一部分包含来自数据源的数据。 However the querystring has a parameter that I need to include in the link.但是,查询字符串有一个我需要包含在链接中的参数。 How can I get scope to my data that holds the querystring value so I can add the querystring to the link in my v-slot ?如何将 scope 获取到包含查询字符串值的数据中,以便我可以将查询字符串添加到我的v-slot中的链接?

Thank you in advance, Marty提前谢谢你,马蒂

<template>
   <div>
      <h1>View Users</h1>
      Select a user to edit

      <b-table striped :items="users">
         <template v-slot:cell(id)="data">
            <a :href="'/#/admin/user?userId=' + data.value + '&companyId=' + ##HERE## ">{{ data.value }}</a>
         </template>
      </b-table>
   </div>
</template>
export default {
  data() {
    return {
      users: [],
      companyId: ""
    }
  },
  methods: {
    getUsers() {
      var self = this;
      self.$client.get('/api/Admin/GetUsers?companyId=' + this.$route.query.companyId).then(response => {
        self._data.users = response.data;
      });
    }
  },
  mounted() {
    this.companyId = this.$route.query.companyId
    this.getUsers();
  }
}

The <a> is parent content that is being passed into the <b-table> slot, and that means it has access to the parent data. <a>是传递到<b-table>槽的父内容,这意味着它可以访问父数据。 So you can access the companyId directly just as you would if there was no <b-table> :因此,您可以像没有<b-table>一样直接访问companyId

<b-table striped :items="users">
   <template v-slot:cell(id)="data">
      <a :href="'/#/admin/user?userId=' + data.value + '&companyId=' + companyId">
      {{ data.value }}
      </a>
   </template>
</b-table>

For route links, it's best to use <router-link> instead of an <a> tag:对于路由链接,最好使用<router-link>而不是<a>标签:

<router-link :to="{
   path: '/admin/user',
   query: { userId: data.value, companyId: companyId }
}">
   {{ data.value }}
</router-link>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM