语言更改后Vue数据变量未更新

时间:2019-04-26 15:04:25

标签: vue.js

我有一个数据变量,如下所示:

data(){
  return {
    amount: {
      active: false,
      name: this.$i18n.t('key_name'),
      value: this.$i18n.t('key_value')
    }
  }
}

数据变量正在使用本地化字符串。问题是,当我从下拉菜单更改语言时,amount变量在发生任何其他事件之前不会更新。我相信这与$nextTick()有关。

但不确定如何正确处理此更改,以使更改立即反映在amount变量中。

我不能将其设为计算属性,因为稍后必须为该变量分配值。

像这样:

this.amount.active = true

我该如何解决?

1 个答案:

答案 0 :(得分:1)

很难说出如何使用Vue i18n,但是您可以使用文档中所述的watcher并注意全局i18n.locale object,例如:

if(pocketItemAdapter.removeItem(viewHolder.getAdapterPosition()) == 1){


                    Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,R.string.successfully_deleted,3000)
                            .setAction(R.string.undo, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    pocketItemAdapter.restoreItem(pocketList,deletetIndex);
                                    database.createpocketEntry(pocketList.getTIMESTAMP(),pocketList.getAMOUNT(),pocketList.getDESCRIPTION(),pocketList.getCATEGORY());
                                }
                            });
                    snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.colorBright));

                    snackbar.show();
                }else{

                    final Snackbar snackbar = Snackbar.make(coordinatorLayoutPocketMain,"Löschen war nicht erfolgreich",Snackbar.LENGTH_SHORT);
                            snackbar.setAction("Ok", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    snackbar.dismiss();
                                }
                            });
                    snackbar.getView().setBackgroundColor(ContextCompat.getColor(PocketMain.this,R.color.red));
                    snackbar.show();
                    pocketItemAdapter.restoreItem(pocketList,deletetIndex);

                }

取决于它在项目中的配置方式。

相关问题