使用Kotlin Multi Platform创建JS库,并通过NPM在Angular 2上使用它

发布于 2025-02-11 03:41:14 字数 2975 浏览 1 评论 0 原文

我创建了简单的Kotlin Multi Platform应用程序,并且想通过在NPM上发布在Angular Project上使用它。

这是我的build.gradle.kts file

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    js {
        browser {
            webpackTask {
                output.libraryTarget = "this"
            }
            binaries.executable()
        }
    }
    
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = 32
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

kmp project中的示例招呼类文件

@JsExport
data class Greeting(val title: String, val row: Int) {

    fun helloWorld() {
        print("TEST Hello World")
    }

}

当我运行gradle build command命令js文件夹时出现在构建文件夹下方及其外观这个。

”在此处输入图像描述”

我从项目中删除了此JS文件夹,然后放入新的GitHub存储库中,然后将新的GitHub存储库发布到NPM。

这是我在NPM上发布的项目。

到目前为止一直很棒

之后,我创建了一个角度项目,并将此库添加为依赖性

"bf-shared-mw": "^1.0.1",

它取决于成功

,但是当我想在下面看到的代码等组件类中使用

import { Component, OnInit } from '@angular/core';
import { Greeting } from 'bf-shared-mw';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnInit {

  ngOnInit(): void {

    const greetings = Greeting();

    
    
  }

  title = 'angular-tour-of-heroes';

}

时i.sstatic.net/ebwmw.png“ rel =“ nofollow noreferrer”>

此处强>我在做什么错?我如何在React或Angular应用程序上使用Kotlin多平台库?

I created and simple kotlin multi platform application and i want to use it on angular project via publishing on npm.

Here is my build.gradle.kts file

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    js {
        browser {
            webpackTask {
                output.libraryTarget = "this"
            }
            binaries.executable()
        }
    }
    
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = 32
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

Sample Greeting Class File in KMP Project

@JsExport
data class Greeting(val title: String, val row: Int) {

    fun helloWorld() {
        print("TEST Hello World")
    }

}

When i run gradle build command js folder appearing under the build folder and its looks like this.

enter image description here

I removed this JS folder from the project and put into the new github repository and then i publish the new github repository to the npm.

Here is the project that i publish on npm.

https://www.npmjs.com/package/bf-shared-mw

It has been great until now

After then i created a angular project and add this library as a dependecy

"bf-shared-mw": "^1.0.1",

It's depended with successfully

But when i want to use it in components classes like the codes as you see below

import { Component, OnInit } from '@angular/core';
import { Greeting } from 'bf-shared-mw';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnInit {

  ngOnInit(): void {

    const greetings = Greeting();

    
    
  }

  title = 'angular-tour-of-heroes';

}

bf-shared-mw Throwing errors and i cant access the Greeting class

enter image description here

What am i doing wrong ? How can i use my kotlin multi platform library on react or angular applications ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文