Export json in javascript function to be imported in another javascript function
Export json in javascript function to be imported in another javascrtipt function
我需要读取 json 文件并使其在整个应用程序中可用。我的方法是(摘自此resource):
assets/js/routes.json:
{
"task.create": "deliveryorders\/{deliveryOrder}\/tasks\/create",
}
assets/js/router.js:
import * as routes from './routes.json';
export function route() {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
if (routes[name] === undefined) {
console.error('Unknown route ', name);
} else {
return baseUrl + '/' + routes[name]
.split('/')
.map(s => s[0] == '{' ? args.shift() : s)
.join('/');
}
}
assets/js/base/pltbl.js:
import '../router.js';
window.tblLoadContent = function tblLoadContent(argModel) {
url_ = route(argModel.name + ".index", [argModel.parent_id]);
$("#tbl" + _titleCase(argModel.name) + "_body").load(url_, function(responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
refreshMenu(argModel);
}
});
}
在app.blade.php中调用:
$(document).ready(function(){
tblLoadContent("{'name':'package', 'parent_id': '1'}");
});
但是当呈现 app.blade.php 时,在控制台中弹出以下错误:
Uncaught ReferenceError: route is not defined
at tblLoadContent (pltbl.js:141)
at HTMLDocument.<anonymous> (edit:473)
at l (jquery-3.3.1.min.js:2)
at c (jquery-3.3.1.min.js:2)
我认为您需要创建默认导出或使用大括号导入路由。
选项 1:
export default function route()
选项 2:
import {route} from '../router.js';
我需要读取 json 文件并使其在整个应用程序中可用。我的方法是(摘自此resource):
assets/js/routes.json:
{
"task.create": "deliveryorders\/{deliveryOrder}\/tasks\/create",
}
assets/js/router.js:
import * as routes from './routes.json';
export function route() {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
if (routes[name] === undefined) {
console.error('Unknown route ', name);
} else {
return baseUrl + '/' + routes[name]
.split('/')
.map(s => s[0] == '{' ? args.shift() : s)
.join('/');
}
}
assets/js/base/pltbl.js:
import '../router.js';
window.tblLoadContent = function tblLoadContent(argModel) {
url_ = route(argModel.name + ".index", [argModel.parent_id]);
$("#tbl" + _titleCase(argModel.name) + "_body").load(url_, function(responseTxt, statusTxt, xhr) {
if (statusTxt == "success") {
refreshMenu(argModel);
}
});
}
在app.blade.php中调用:
$(document).ready(function(){
tblLoadContent("{'name':'package', 'parent_id': '1'}");
});
但是当呈现 app.blade.php 时,在控制台中弹出以下错误:
Uncaught ReferenceError: route is not defined
at tblLoadContent (pltbl.js:141)
at HTMLDocument.<anonymous> (edit:473)
at l (jquery-3.3.1.min.js:2)
at c (jquery-3.3.1.min.js:2)
我认为您需要创建默认导出或使用大括号导入路由。 选项 1:
export default function route()
选项 2:
import {route} from '../router.js';