qq_笑_17
您可以使用两种访问器访问对象(导入或本地)的属性:点表示法和括号表示法。下面的 Mozilla 文档清楚地解释了它们。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors在访问对象内部的属性时,括号表示法提供了更多的自由。假设您有一个带有语言选择器下拉列表和数据表的 Vue 组件。你的data块看起来像这样:data() { return { language: "en", languageOptions: ["en", "es"], headers: [ { text: "Name", sortable: true, value: "name" }, { text: "Lastname", sortable: true, value: "last_name" }, { text: "Email", sortable: true, value: "email" }, { text: "Role", sortable: true, value: "roles[0].name" }, { text: "Actions", sortable: false, align: "center" } ], }}对于您的messages.js文件:export default { en: { name: "Name", lastName: "Last Name", role: "Role", email: "Email", actions: "Actions" }, es: { name: "Nombre", number: "Numero", role: "El Oficio" email: "Email", actions: "Acción" }}用来自相应语言选项的值替换标题文本messages[language][fieldName]:修改后的代码:data() { return { language: "en", languageOptions: ["en", "es"], headers: [ { text: messages[language]["name"], sortable: true, value: "name" }, { text: messages[language]["lastName"], sortable: true, value: "last_name" }, { text: messages[language]["email"], sortable: true, value: "email" }, { text: messages[language]["role"], sortable: true, value: "roles[0].name" }, { text: messages[language]["actions"], sortable: false, align: "center" } ], }}我还建议查看 Vue 的本地化插件vue-i18n。