I have a requirement to read a json file and make it available across the application. My approach was (extracted from this 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);
}
});
}
It is called in app.blade.php :
$(document).ready(function(){
tblLoadContent("{'name':'package', 'parent_id': '1'}");
});
But when the app.blade.php is rendered, pops below error in console:
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)
I think you need or to create a default export or to import the route with curly braces. Option 1:
export default function route()
Option 2:
import {route} from '../router.js';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.