【问题标题】:translate typescript to javascript [closed]将打字稿翻译成javascript [关闭]
【发布时间】:2017-11-07 08:38:06
【问题描述】:

我需要此代码在 Javascript

我不擅长语言打字。

这是使用 nativescript 在移动设备中播放音乐和录制音频的代码

不要以为 GitHub 很快就给出答案

in github issues

import { TNSPlayer } from 'nativescript-audio';

export class YourClass {
    private _player: TNSPlayer;

    constructor() {
        this._player = new TNSPlayer();
        this._player.initFromFile({
            audioFile: '~/audio/song.mp3', // ~ = app directory
            loop: false,
            completeCallback: this._trackComplete.bind(this),
            errorCallback: this._trackError.bind(this)
        }).then(() => {

            this._player.getAudioTrackDuration().then((duration) => {
                // iOS: duration is in seconds
                // Android: duration is in milliseconds
                console.log(`song duration:`, duration);
            });
        });
    }

    public togglePlay() {
        if (this._player.isAudioPlaying()) {
            this._player.pause();
        } else {
            this._player.play();
        }
    }

    private _trackComplete(args: any) {
        console.log('reference back to player:', args.player);

        // iOS only: flag indicating if completed succesfully
        console.log('whether song play completed successfully:', args.flag);
    }

    private _trackError(args: any) {
        console.log('reference back to player:', args.player);
        console.log('the error:', args.error);

        // Android only: extra detail on error
        console.log('extra info on the error:', args.extra);
    }
}

【问题讨论】:

    标签: javascript typescript nativescript translate


    【解决方案1】:

    如果您使用 Webpack 或任何 Angular (2+) 种子项目,甚至是 angular-cli,您的所有 TypeScript 代码都将“编译”为 ECMAScript,您可以选择版本(从 5 到 7)。

    只需打开 tsconfig.json。 target 会给出你需要的具体版本的 ECMAScript。

     "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ]
    

    运行 build 并从 outDir 目录中获取您的 JavaScript 文件。

    【讨论】:

    • 我不想使用 TypeScript 语言。我想把代码直接转成 JavaScript 编程语言
    【解决方案2】:

    您可以使用 TypeScript 编译器 (tsc) 来执行此操作。对于您的 sn-p,输出是这样的:

    "use strict";
    exports.__esModule = true;
    var nativescript_audio_1 = require("nativescript-audio");
    var YourClass = (function () {
        function YourClass() {
            var _this = this;
            this._player = new nativescript_audio_1.TNSPlayer();
            this._player.initFromFile({
                audioFile: '~/audio/song.mp3',
                loop: false,
                completeCallback: this._trackComplete.bind(this),
                errorCallback: this._trackError.bind(this)
            }).then(function () {
                _this._player.getAudioTrackDuration().then(function (duration) {
                    // iOS: duration is in seconds
                    // Android: duration is in milliseconds
                    console.log("song duration:", duration);
                });
            });
        }
        YourClass.prototype.togglePlay = function () {
            if (this._player.isAudioPlaying()) {
                this._player.pause();
            }
            else {
                this._player.play();
            }
        };
        YourClass.prototype._trackComplete = function (args) {
            console.log('reference back to player:', args.player);
            // iOS only: flag indicating if completed succesfully
            console.log('whether song play completed successfully:', args.flag);
        };
        YourClass.prototype._trackError = function (args) {
            console.log('reference back to player:', args.player);
            console.log('the error:', args.error);
            // Android only: extra detail on error
            console.log('extra info on the error:', args.extra);
        };
        return YourClass;
    }());
    exports.YourClass = YourClass;
    

    【讨论】:

      【解决方案3】:

      您可以使用 typescript 游乐场将 typescript 即时转换为 javascript。 这是网址http://www.typescriptlang.org/play

      或者你可以在你的机器上安装 typescript 编译器并运行 tsc 。

      【讨论】:

      • 我确实在询问之前进行了搜索。表示输出不起作用
      • 这是因为 nativescript-audio 本身就是一个库。这种转换不会下载库,它只会转换它的代码。
      猜你喜欢
      • 1970-01-01
      • 2022-06-14
      • 2023-01-24
      • 1970-01-01
      • 2019-02-11
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多