Vue.js 如何将数据添加到Kotlin对象并在Vuejs页面上获取它
我对科特林绝对是新手。我试图通过Kotlin在后端创建一个简单对象,并在前端Vuejs获取它。我怎么能这样做(这是Vue.js 如何将数据添加到Kotlin对象并在Vuejs页面上获取它,vue.js,kotlin,Vue.js,Kotlin,我对科特林绝对是新手。我试图通过Kotlin在后端创建一个简单对象,并在前端Vuejs获取它。我怎么能这样做(这是HeaderBar.kt的原始代码,我所有的尝试都被编译器拒绝了): 首先,这不是一个简单的问题。Kotlin/Js不如Kotlin/Jvm成熟,因此有许多不那么简单的任务 首先需要以某种方式编译为javascript,然后需要将Vue附加到kotlin/javascript代码 Webpack可以让它更简单,所以我写了一个简单的例子,向您展示如何用Kotlin编写示例 !警告!:下
HeaderBar.kt
的原始代码,我所有的尝试都被编译器拒绝了):
首先,这不是一个简单的问题。Kotlin/Js不如Kotlin/Jvm成熟,因此有许多不那么简单的任务 首先需要以某种方式编译为javascript,然后需要将
Vue
附加到kotlin/javascript代码
Webpack
可以让它更简单,所以我写了一个简单的例子,向您展示如何用Kotlin编写示例
!警告!:下面的所有代码都只是草稿(并且只是为了演示目的编写的),所以在项目中使用时要特别小心强>
让我们创建具有以下结构的项目:
Application.kt:
package vue_test
fun VueJs(init: VueContext.() -> Unit) = Vue(VueContext().apply(init))
class VueContext {
var el: String = ""
var data: dynamic = js("{}")
}
fun main(args: Array<String>) {
val app: dynamic = VueJs {
el = "#app"
data = mapOf("items" to listOf(
Item("NY", "Bill"),
Item("Test", "Test2")
)).toJs()
}
}
data class Item(
val city: String,
val name: String
)
fun Map<String, Any>.toJs(): dynamic {
val result: dynamic = object {}
for ((key, value) in this) {
when (value) {
is String -> result[key] = value
is List<*> -> result[key] = (value as List<Any>).toJs()
else -> throw RuntimeException("value has invalid type")
}
}
return result
}
fun List<Any>.toJs(): dynamic {
val result: dynamic = js("[]")
for (value in this) {
when (value) {
is String -> result.push(value)
is Item -> {
result.push(value.toJs())
}
else -> throw RuntimeException("value has invalid type")
}
}
return result
}
fun Item.toJs(): dynamic {
val result: dynamic = object {}
result["city"] = this.city
result["name"] = this.name
return result
}
在此文件中,我们只有Vue声明
index.html
<!DOCTYPE html>
<html>
<head>
<title>Test project</title>
</head>
<body class="testApp">
<h1>Kotlin-Js test</h1>
<div id="app">
<div class="main-header">
<div v-for="item in items" class="items">
<span class="city">{{item.city}}</span>
<span class="name">{{item.name}}</span>
</div>
</div>
</div>
<script type="text/javascript" language="JavaScript" src="frontend.bundle.js"></script>
</body>
</html>
格雷德尔酒店
rootProject.name = 'test-kotlin-vue'
最后一个文件,自定义网页包配置:
var config = require('./build/WebPackHelper.js')
var path = require('path')
module.exports = {
entry: config.moduleName,
output: {
path: path.resolve('./bundle'),
publicPath: '/build/',
filename: 'frontend.bundle.js'
},
module: {
rules: []
},
resolve: {
modules: [path.resolve('js'), path.resolve('..', 'src'), path.resolve('.'), path.resolve('node_modules')],
extensions: ['.js', '.css'],
alias: {
'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
}
},
devtool: '#source-map'
};
console.log(module.exports.resolve.modules);
使用kotlin frontend
插件,您无法使用单独的webpack配置,但在本例中Vue
需要完整版本来编译模板,因此需要在webpack中添加别名。我不知道如何在build.gradle中实现这一点
希望这对你有帮助
要使用开发包启动项目,请运行以下命令:gradle build webpack run
,然后打开http://localhost:8080
在浏览器中
停止测试运行命令:gradle webpack stop
<!DOCTYPE html>
<html>
<head>
<title>Test project</title>
</head>
<body class="testApp">
<h1>Kotlin-Js test</h1>
<div id="app">
<div class="main-header">
<div v-for="item in items" class="items">
<span class="city">{{item.city}}</span>
<span class="name">{{item.name}}</span>
</div>
</div>
</div>
<script type="text/javascript" language="JavaScript" src="frontend.bundle.js"></script>
</body>
</html>
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
mavenCentral()
jcenter()
maven {
url "https://dl.bintray.com/kotlin/kotlin-eap"
}
maven {
url "https://repo.gradle.org/gradle/libs-releases-local"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.21"
}
}
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'kotlin-platform-js'
apply plugin: 'org.jetbrains.kotlin.frontend'
repositories {
mavenCentral()
}
kotlinFrontend {
sourceMaps = true
npm {
dependency("vue")
}
webpackBundle {
port = 8080
bundleName = "frontend"
contentPath = file('src/main/web')
webpackConfigFile = project.projectDir.path + '/webpack.config.js'
}
}
compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
kotlin {
experimental {
coroutines 'enable'
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
rootProject.name = 'test-kotlin-vue'
var config = require('./build/WebPackHelper.js')
var path = require('path')
module.exports = {
entry: config.moduleName,
output: {
path: path.resolve('./bundle'),
publicPath: '/build/',
filename: 'frontend.bundle.js'
},
module: {
rules: []
},
resolve: {
modules: [path.resolve('js'), path.resolve('..', 'src'), path.resolve('.'), path.resolve('node_modules')],
extensions: ['.js', '.css'],
alias: {
'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
}
},
devtool: '#source-map'
};
console.log(module.exports.resolve.modules);