在另一个js文件中使用Variable from for function
Use Variable from for function in another js file
我想使用来自其他 js 文件的变量,但是当我使用导入和导出语句时不起作用,因为没有定义 {file}。
我的问题是我的变量在一个函数中,并且在一个循环中。
我想我做错了,但你能帮我吗?
这是我 fabric.js
上的代码
const initCanvas = () => (
new fabric.Canvas('canvas', {
preserveObjectStacking:true,
height: 450,
width: 600,
backgroundImage: {file}, <--- i want to use the variable "file" here
})
)
在我的 emil.js 中,我有一个使用此代码的函数
DisplayCategory(category) {
(...) //Function that generate te list of
else { //Generate the product list of a category (ex : product[2][x])
let product = [];
let x = findProduct(product_array, selected_product);
for (let y = 1; product_array[x][y]; y++) {
var ext = ".jpg";
var file = '/' + product_array[x][y] + ext; <--- file is defined here
product.push(
<Card style={{ width: '18rem' }} className={this.takeClass(product_array[x][y])} onClick={() => this.HandleOnClick(product_array[x][y], 2)}>
<Card.Img variant="top" src={file} /> <--- and is used here
函数太长了,剩下的就不放了
一个常见的模式是有一个共同的命名空间,一个对象,写入window
。这样你的项目只写一个东西到全局命名空间,所有东西都在下面。
这是一种在脚本之间共享数据的简单方法。
假设我们有 bootstrap.js,它这样做:
window.someNamespace = {};
然后在另一个脚本中,你有这个:
someNamespace.sharedProperty = 'foo';
alert('This script can call @sharedProperty -'+someNamespace.sharedProperty);
然后在另一个脚本中,您也可以阅读 属性:
console.log(someNamespace.sharedProperty);
...因为所有内容(或至少需要共享的所有内容 - 其他所有内容都应在闭包中保持本地化)都在 window.someNamespace
.
下
我想使用来自其他 js 文件的变量,但是当我使用导入和导出语句时不起作用,因为没有定义 {file}。 我的问题是我的变量在一个函数中,并且在一个循环中。 我想我做错了,但你能帮我吗?
这是我 fabric.js
上的代码 const initCanvas = () => (
new fabric.Canvas('canvas', {
preserveObjectStacking:true,
height: 450,
width: 600,
backgroundImage: {file}, <--- i want to use the variable "file" here
})
)
在我的 emil.js 中,我有一个使用此代码的函数
DisplayCategory(category) {
(...) //Function that generate te list of
else { //Generate the product list of a category (ex : product[2][x])
let product = [];
let x = findProduct(product_array, selected_product);
for (let y = 1; product_array[x][y]; y++) {
var ext = ".jpg";
var file = '/' + product_array[x][y] + ext; <--- file is defined here
product.push(
<Card style={{ width: '18rem' }} className={this.takeClass(product_array[x][y])} onClick={() => this.HandleOnClick(product_array[x][y], 2)}>
<Card.Img variant="top" src={file} /> <--- and is used here
函数太长了,剩下的就不放了
一个常见的模式是有一个共同的命名空间,一个对象,写入window
。这样你的项目只写一个东西到全局命名空间,所有东西都在下面。
这是一种在脚本之间共享数据的简单方法。
假设我们有 bootstrap.js,它这样做:
window.someNamespace = {};
然后在另一个脚本中,你有这个:
someNamespace.sharedProperty = 'foo';
alert('This script can call @sharedProperty -'+someNamespace.sharedProperty);
然后在另一个脚本中,您也可以阅读 属性:
console.log(someNamespace.sharedProperty);
...因为所有内容(或至少需要共享的所有内容 - 其他所有内容都应在闭包中保持本地化)都在 window.someNamespace
.