html - vue + 纯 html 组件?
问题描述
如何实现 vue-cli 调用纯 html 方法?
请检查下面的示例文件。因为我有自定义的纯 html 代码并且在将其转换为 vuejs 时遇到了困难。
产品.vue
<template>
<custombutton @childclick="childclick" />
</template>
<script>
export default {
component: { custombutton },
methods: {
childclick(value) {
console.log(value)
}
}
}
</script>
自定义按钮.html
<html>
<body>
<button @click="childclick" />
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
<script>
var App = new Vue({
el: '#app',
data() {
return {
vueMessage: 'testing vue message',
}
},
methods: {
childclick() {
this.$emit('childclick', 'success!')
}
},
});
</script>
</html>
解决方案
This solution is a bit more cumbersome, but allows template to be written with syntax highlighting into <script>
element, which is more convenient than writing template as a string literal into a template
property.
custombutton.js
:
export default {
name: 'CustomButton',
data: function () {
return { testVar: 125 }
}
}
custombutton.component.html
:
The template is now located in #custombutton-template
element. "Hello world! 125" should be printed out.
<!DOCTYPE html>
<html>
<head>
<title>App</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<script id="custombutton-template" type="x-template">
<div>Hello world! {{ testVar }}</div>
</script>
<div id="app"></div>
<script type="module">
import CustomButton from "./custombutton.js"
CustomButton.template = "#custombutton-template"
new Vue({
el: '#app',
components: { CustomButton },
template: '<CustomButton />'
});
</script>
</body>
</html>
product.vue
: Thanks to Webpack config (see below) we are able to import contents of custombutton.component.html
file as a string, parse it and get inner HTML of #custombutton-template
element, which is the desired template.
<template>
<CustomButton />
</template>
<script>
import CustomButton from "./custombutton.js"
import CustomButtonComponent from "./custombutton.component.html"
var el = document.createElement("html")
el.innerHTML = CustomButtonComponent
var template = el.querySelector("#custombutton-template").innerHTML
CustomButton.template = template
export default {
components: { CustomButton }
}
</script>
Importing custombutton.component.html
file as string is done by non-default Webpack raw-loader
, therefore it is necessary to adjust Webpack config:
vue.config.js
:
module.exports = {
runtimeCompiler: true,
configureWebpack: {
module: {
rules: [
{
test: /\.component\.html$/,
use: ["raw-loader"]
}
]
}
}
}
package.json
:
{
"devDependencies": {
"raw-loader": "^4.0.2"
}
}
推荐阅读
- javascript - 嵌套数组内的映射数组
- reactjs - 使用 Apple id 登录会返回“invalid_request 无效的 Web 重定向 url”。
- mysql - MySQL 未知错误:重命名 3GB 表时为 -1(无外键)
- java - 如何在Java中的二维字符数组的mutator(setter)方法中传递值?
- qt - QML 启动外部程序:使用 QProcess 失败构建
- spring-boot - Spring Data MongoDB 无法使用我的存储库测试的@Transactional:无法为@Transactional 测试检索 PlatformTransactionManager
- lisp - 将文档字符串合并到 def* 宏中的正确方法?
- c# - 如何使用 Enum.GetValues() 方法的结果?
- python - 无法在 Ubuntu 中使用 Python 中的标准输入
- c++ - 我对 std::shared_ptr 删除器有疑问