javascript - 如何在外部 json 文件上为 Vue js 上的 Web 应用程序最终构建创建 require 函数?
问题描述
我正在构建一个仅在机器上本地服务的测验网络应用程序。现在所有的逻辑和架构都完成了。但剩下的唯一问题是:我如何使“questionOptions.js”中的问题列表在生产版本中成为外部的?这样我就可以更改列表而无需导出另一个构建版本?
目前,该列表正在以开发模式从本地提取,我不知道将列表加载为动态(可以在最终构建时进行编辑)。
Vue.js
var looper = new Vue({
el: "#quiz",
data: {
questionList: require("./js/questionsOptions"),
currentQuestion: 0,
...
}
});
questionOptions.js
module.exports = [
{
title: "Soalan 1",
questionTitle: 'Ini adalah penyata soalan 1',
correctAnswer: true,
answerSelection: [
{
name: "Answer 1 A",
score: true
},
{
name: "Answer 1 B",
score: false
},
]
},
{
title: "Soalan 2",
questionTitle: 'Ini adalah penyata soalan 2',
correctAnswer: true,
answerSelection: [
{
name: "Answer 2 A",
score: true
},
{
name: "Answer 2 B",
score: false
}
]
}
]
我将来可能想更改列表内容或长度,仅在最终版本中更改问题列表。我怎样才能做到这一点?
解决方案
require()
is synchronous and executes at build time.
To perform an async request at runtime, you can use import()
. Keep in mind, your app will need to be able to handle the asynchronous loading.
For example
data: {
questionList: [],
currentQuestion: 0,
},
async created () {
this.questionList = await import(
/* webpackChunkName: "questionOptions" */
'./js/questionsOptions'
)
}
推荐阅读
- angular - 防止 mat-datepicker 关闭日期选择
- lua - awesome.util.spawn 两次执行命令 - Awesome WM
- google-sheets - 如何按列值(月份名称)、数字和查询语句本身对 Google 表格查询结果进行排序?
- sql-server - SQL server 查询输出如下所示的数据
- php - 为什么wordpress循环中有if语句
- javascript - 使用 HTML 标签连接 React 状态值
- parsing - 未捕获的类型错误:无法读取赛普拉斯中未定义的属性“解析”
- linux - 为什么子目录中的文件没有被复制?
- javascript - 刷新页面后Angular 8松散数据
- kotlin - Quarkus kotlin 应用程序中无法注入 REST 客户端接口