在打字稿中安装模块

问题描述 投票:1回答:1

我打算在打字稿中安装和使用victory-native模块。我试过npm install @types/victory-native,但它说没有模块@types/victory-native

我应该只在打字稿中安装@types/...模块吗?

如何在打字稿中使用此模块?

typescript react-native typescript2.0
1个回答
1
投票

只是做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来安装模块
  • 在tsconfig.json中,设置"allowJs": true, "noImplicitAny": false
  • react-native link react-native-svg之后,打开Android Studio,在build.gradle(Module:app)文件中,尝试在底部找到dependencies { compile project(':react-native-svg') ... }。添加此行,如果它不存在。
  • 在android文件夹下,在命令行中运行gradlew clean
  • 然后构建您的js文件,并从命令行运行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;
© www.soinside.com 2019 - 2024. All rights reserved.