将 require 与 Typescript 和 Expo 一起使用时获得 "Invalid call"
Getting "Invalid call" when using require with Typescript and Expo
我正在尝试在使用 expo-cli 创建的 react-native 应用程序中播放一些音频。
代码是用 typescript 编写的,违规代码如下所示,取自 expo.io documentation:
import * as React from 'react'
import { WorkoutComponent } from "./WorkoutExecutor";
import { Audio } from 'expo';
export default class AudioPlayer {
private async playAudio(fileName: string) {
console.log("Playing Audio: " + fileName);
const soundFile = './assets/sounds/' + fileName + '.mp3';
try {
const { sound: soundObject, status } = await Audio.Sound.createAsync(
require(soundFile),
{ shouldPlay: true }
);
// Your sound is playing!
} catch (error) {
console.log(error);
// An error occurred!
}
}
[...]
}
当应用程序加载时,它会出现以下错误,甚至在它到达屏幕之前就出现了声音
[...]\src\AudioPlayer.ts:Invalid call at line 13: require(soundFile)
我知道 coe 示例是 javascript 而不是打字稿,但我错过了什么?
我的 tsconfig.json 是 expo typescript 示例中的那个,看起来像这样
{
"compilerOptions": {
"baseUrl": "./src",
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"jsx": "react-native",
"module": "es2015",
"moduleResolution": "node",
"noEmitHelpers": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
// Using the type definitions in @types/expo becuase they are still better than the ones provided by expo. See SvgScreen.tsx and SystemFontsScreen.tsx.
"paths": {
"expo": [
"../node_modules/@types/expo",
"../node_modules/expo"
],
},
"skipLibCheck": true,
"strict": true,
"target": "es2017"
},
"exclude": [
"node_modules"
]
}
我通过 Twitter 获得了帮助,问题是 require()
不适用于动态值。这也解释了为什么错误甚至在应用程序加载之前就发生了,因为它试图在构建或加载时而不是像我想象的那样在运行时解析 require
。
我用
修复了它
import song from "./path_to_song/song.mp3"
import * as React from 'react'
import { WorkoutComponent } from "./WorkoutExecutor";
import { Audio } from 'expo';
const fileA = require('path/to/sound/file.mp3');
const fileB = require('path/to/sound/file2.mp3');
export default class AudioPlayer {
private async playAudio(fileName: string) {
if (fileName == 'A') {
var soundFile = fileA;
} else {
var soundFile = fileB;
}
try {
const { sound: soundObject, status } = await Audio.Sound.createAsync(
soundFile,
{ shouldPlay: true }
);
// Your sound is playing!
} catch (error) {
console.log(error);
// An error occurred!
}
}
[...]
}
我正在尝试在使用 expo-cli 创建的 react-native 应用程序中播放一些音频。
代码是用 typescript 编写的,违规代码如下所示,取自 expo.io documentation:
import * as React from 'react'
import { WorkoutComponent } from "./WorkoutExecutor";
import { Audio } from 'expo';
export default class AudioPlayer {
private async playAudio(fileName: string) {
console.log("Playing Audio: " + fileName);
const soundFile = './assets/sounds/' + fileName + '.mp3';
try {
const { sound: soundObject, status } = await Audio.Sound.createAsync(
require(soundFile),
{ shouldPlay: true }
);
// Your sound is playing!
} catch (error) {
console.log(error);
// An error occurred!
}
}
[...]
}
当应用程序加载时,它会出现以下错误,甚至在它到达屏幕之前就出现了声音
[...]\src\AudioPlayer.ts:Invalid call at line 13: require(soundFile)
我知道 coe 示例是 javascript 而不是打字稿,但我错过了什么?
我的 tsconfig.json 是 expo typescript 示例中的那个,看起来像这样
{
"compilerOptions": {
"baseUrl": "./src",
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"jsx": "react-native",
"module": "es2015",
"moduleResolution": "node",
"noEmitHelpers": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
// Using the type definitions in @types/expo becuase they are still better than the ones provided by expo. See SvgScreen.tsx and SystemFontsScreen.tsx.
"paths": {
"expo": [
"../node_modules/@types/expo",
"../node_modules/expo"
],
},
"skipLibCheck": true,
"strict": true,
"target": "es2017"
},
"exclude": [
"node_modules"
]
}
我通过 Twitter 获得了帮助,问题是 require()
不适用于动态值。这也解释了为什么错误甚至在应用程序加载之前就发生了,因为它试图在构建或加载时而不是像我想象的那样在运行时解析 require
。
我用
修复了它import song from "./path_to_song/song.mp3"
import * as React from 'react'
import { WorkoutComponent } from "./WorkoutExecutor";
import { Audio } from 'expo';
const fileA = require('path/to/sound/file.mp3');
const fileB = require('path/to/sound/file2.mp3');
export default class AudioPlayer {
private async playAudio(fileName: string) {
if (fileName == 'A') {
var soundFile = fileA;
} else {
var soundFile = fileB;
}
try {
const { sound: soundObject, status } = await Audio.Sound.createAsync(
soundFile,
{ shouldPlay: true }
);
// Your sound is playing!
} catch (error) {
console.log(error);
// An error occurred!
}
}
[...]
}