1
我曾嘗試使用在一個硬編碼字符串代替使用變量不工作要求()
const myPath = './TpsComponent2';
const ms = require(myPath);
這是行不通的。但是,當我嘗試,
const ms = require('./TpsComponent2');
它的工作!
我缺少什麼?我想將路徑作爲變量傳遞。
我曾嘗試使用在一個硬編碼字符串代替使用變量不工作要求()
const myPath = './TpsComponent2';
const ms = require(myPath);
這是行不通的。但是,當我嘗試,
const ms = require('./TpsComponent2');
它的工作!
我缺少什麼?我想將路徑作爲變量傳遞。
React原生應用程序是Javascript應用程序(可選用本機代碼)。在設備上運行之前,它被捆綁在一起 - ES6被轉移到ES5,資產也被捆綁在一起。
這發生在運行應用程序之前。因此,必須在編譯時間之前知道所有require
。你不能在RN應用程序中以這種方式要求組件(或其他)。
在純節點環境中,這是沒有問題的。