我打算在打字稿中安装和使用victory-native模块。我试过npm install @types/victory-native
,但它说没有模块@types/victory-native
。
我应该只在打字稿中安装@types/...
模块吗?
如何在打字稿中使用此模块?
只是做npm install victory-native --save
这个模块没有打字,但没有什么可以阻止你使用它。
您还需要手动安装依赖项,然后链接本机代码
react-native link react-native-svg
。
请参考他们的指南:https://github.com/FormidableLabs/victory-native
更新 如果您不熟悉如何使用本机模块和TypeScript。 这种情况有我的方法:
create-react-native-app
创建的。确保通过运行yarn eject
弹出项目yarn add victory-native react-native-svg
来安装模块"allowJs": true, "noImplicitAny": false
。react-native link react-native-svg
之后,打开Android Studio,在build.gradle(Module:app)文件中,尝试在底部找到dependencies { compile project(':react-native-svg') ... }
。添加此行,如果它不存在。gradlew clean
。react-native run-android
。这个模块现在应该工作。有我的tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"jsx": "react-native",
"outDir": "build",
"rootDir": "src",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"noImplicitAny": false,
"preserveConstEnums": true,
"allowJs": true,
"sourceMap": false,
"noImplicitReturns": true,
"noUnusedParameters": true,
"noUnusedLocals": true
},
"filesGlob": ["typings/index.d.ts", "src/**/*.ts", "src/**/*.tsx"],
"types": ["react", "react-native", "jest"],
"exclude": ["android", "ios", "build", "node_modules"],
"compileOnSave": false,
"include": ["src/**/*"]
}
一个直接从official documentation复制的简单示例:
import React, { Component } from "react";
import { VictoryBar } from "victory-native";
class App extends Component {
render() {
return (
<VictoryBar />
);
}
}
export default App;