简体   繁体   English

bootstrap vue b-table上模板内的Vue'this'对象

[英]Vue 'this' object inside template on bootstrap vue b-table

I added a set of functions into我添加了一组函数

Vue.prototype.elantana = require("./shared/elantana.js").default;

at my main.js;在我的 main.js; since I need this functions all alone my app.因为我需要单独使用这个功能我的应用程序。

Accessing to this set of functions is a piece of cake.访问这组功能是小菜一碟。 I pass the 'this' object as a param to acces the vue object我将“this”对象作为参数传递给 vue 对象

<div v-if="elantana.CheckPermission(this, 'fabrics@add_fabric')">
    <b-button class="float-button" variant="warning" @click="ShowEditModal">+</b-button>
</div>

Problem comes when running this same function into a b-table component template:将相同的函数运行到 b-table 组件模板中时会出现问题:

<b-table class="table-outline bg-white" :hover="true" :striped="false" :bordered="false" :small="false" :fixed="false" responsive="sm" :items="fabrics" :fields="tableFields" :current-page="currentPage" :per-page="perPage" head-variant="light">
    <template slot="actions" slot-scope="data">
        <div class="float-right">
            <div v-if="elantana.CheckPermission(this, 'fabrics@edit_fabric')">
                <b-button class="mr-2" type="button" size="sm" variant="danger" @click="ShowEditModal(data.item)"><i class="fa fa-trash-o"></i></b-button>
            </div>
        </div>
    </template>
</b-table>

[Vue warn]: Error in render: "TypeError: Cannot read property '$store' of null" [Vue 警告]:渲染错误:“TypeError:无法读取 null 的属性‘$store’”

This is the prototype of the function这是函数的原型

/**
 * Returs if the permissions is in the avaialable permissions array
 * @param {VUE object} vm 
 * @param {String} permission 
 * @return {Boolean} 
 */
CheckPermission(vm, permission)

I thought in overpass the issue with a method inside the component acting as a proxy for the main function, or creating a prop in the component that returns the "this" object我认为通过组件内部的方法作为主函数的代理来解决问题,或者在组件中创建一个返回“this”对象的道具

Any way to use the "this" object inside bootstrap b-table template?有什么方法可以在 bootstrap b-table 模板中使用“this”对象?

This solution is not completely valid;此解决方案并不完全有效; since you can access the vie object, you can not access the “this” object of the component.由于您可以访问 vie 对象,因此您无法访问组件的“this”对象。 To do so you need to set the VM object in every mounted hook of each component.为此,您需要在每个组件的每个挂载钩子中设置 VM 对象。

I'll guess there is a better solution for this.我猜有一个更好的解决方案。 Any ideas?有任何想法吗?

Just fixed it using a property inside my module:只是使用我的模块中的一个属性修复了它:

VM: null,

SetVM(vm) {
    this.VM = vm;
},

Then, in every other function i use然后,在我使用的所有其他功能中

this.VM

Then there is no need anymore to pass the vue object as a parameter to the function.这样就不需要再将 vue 对象作为参数传递给函数了。 hence, fixing the issue on the b-table template on bootstrap-vue因此,修复了 bootstrap-vue 上 b-table 模板的问题

What I just did to solve the same issue is to call "local" method defined in scirpts of the .vue page:我刚刚为解决同样的问题所做的是调用 .vue 页面的脚本中定义的“本地”方法:

  <b-table /*...*/>
    <template #cell(foo)="data">
      {{ localFormat(data.value) }}
    </template>
  </b-table>

and from there just call the target "global" method, because Vue object is easilly accesible here:然后从那里调用目标“全局”方法,因为 Vue 对象在这里很容易访问:

...
<script>
...
  methods: {
    localFormat(value) {
      // cannot call "Vue" object from inside of BootstrapVue template
      return this.globalFormat(value);
    },
  },
...
</script>

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

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