您只需要在创建组件时设置 helpTitles 属性。
我建议在您的模板中使用$t(),而不是在data() 中。然后它会自动对更改做出反应。
老实说,我不认为使用翻译文件中的数组是一个好主意。我更倾向于用自己的键添加它们,就像您的问题和信息翻译键一样,例如
helpStartedTitle: "GETTING STARTED - MODEL",
helpMembersTitle: "MEMBERS",
helpAccountTitle: "ACCOUNT",
//etc
然后您可以像这样在数据中设置键
data: () => {
const keys = [
"helpStarted",
"helpMembers",
"helpAccount",
"helpPayment",
"helpSocial",
"helpFraud",
"helpSupport",
"helpStudio"
]
return {
helpInfo: keys.map((key, id) => ({
id,
title: `general.help.${key}Title`,
question: `general.help.${key}`,
answer: `general.help.${key}Info`
}))
}
}
然后在你的模板中
<div v-for="help in helpInfo" :key="help.id">
<div :id="help.id" class="help-subtitle">{{ $t(help.title) }}:</div>
<HelpItem
:question="$t(help.question)"
:answer="$t(help.answer)"
:num="help.id"
/>
</div>
最好将翻译键传递给您的HelpItem 组件并使用$t()。
<HelpItem
:question="help.question"
:answer="help.answer"
:num="help.id"
/>
在HelpItem 组件中
export default {
name: "HelpItem",
props: {
question: String,
answer: String,
num: Number
},
// etc
}
<!-- just guessing here -->
<h1>{{ $t(question) }}</h1>
<p>{{ $t(answer) }}</p>
仅供参考,我已将 answear 更正为 answer。