【问题标题】:Wrapping Selectpicker with Vuejs Directive and i18n使用 Vuejs 指令和 i18n 包装 Selectpicker
【发布时间】:2023-03-06 10:22:01
【问题描述】:

有人可以帮助我吗?

我正在做一个 Vue.js 指令来包装 bootstrap-selectpicker 并在指令内部我想使用 vue-i18n-plugin 来翻译默认标题,以防此参数为空但我不能......我我收到一条错误消息:

Uncaught ReferenceError: $i18n is not defined

my-directive.js

import Vue from 'vue';

Vue.directive('plSelectpicker', {
  twoWay: true,

  params: [
    'title',
    'style',
    'liveSearch',
    'actionsBox'
  ],

  bind: function() {
    $(this.el).selectpicker({
      liveSearch: this.params.title || true,
      title: this.params.title || $i18n.select,
      style: this.params.style || 'btn-white',
      actionsBox: this.params.actionsBox || true,
    });
  },
});

我也试过那个:

import Vue from 'vue';
import i18n from 'vue-i18n-plugin/src/vue-i18n.js';
Vue.use(i18n);

Vue.directive('plSelectpicker', {
  twoWay: true,

  params: [
    'title',
    'style',
    'liveSearch',
    'actionsBox'
  ],

  bind: function() {
    $(this.el).selectpicker({
      liveSearch: this.params.title || true,
      title: this.params.title || $i18n.select, // also tried this.$i18n
      style: this.params.style || 'btn-white',
      actionsBox: this.params.actionsBox || true,
    });
  },
});

但它让我陷入了错误:

未捕获的类型错误:无法读取未定义的属性“选择”

有人知道吗?

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    this 指的是指令,而不是 Vue 实例,所以 $i18n 不能以这种方式访问​​。试试this.vm.$i18n

    【讨论】:

    • 酷!它可以工作,但我仍然有一个问题......当我更改语言应用程序时,这个标题不会改变!你知道为什么吗?所有的应用程序都在改变,但它没有!
    • selectpicker 标题不像 Vue 那样响应。您需要使用指令的 update 函数来更新、刷新或使用新选项重建选择选择器
    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 2019-09-28
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2020-04-01
    • 2018-06-05
    相关资源
    最近更新 更多