<html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue</title> <script src="https://www.hxstrive.com/cdn/libs/vue/2.7.16/vue.min.js"></script> </head> <body> <div id="app"> <h1>title: {{ obj.title }}, summary: {{ obj.summary }}</h1> <!-- 绑定 update:title 自定义事件 --> <my-component v-bind.sync="obj"></my-component> </div> <script type="text/javascript"> Vue.component('my-component', { data: function(){ return { title2: this.$attrs.title, summary2: this.$attrs.summary }; }, template: ` <div> <p> <input type="text" placeholder="Enter title" v-model="title2" /> </p> <p> <input type="text" placeholder="Enter summary" v-model="summary2" /> </p> <p> <button v-on:click="sendData">Send Data</button> </p> </div> `, methods: { sendData: function(){ this.$emit('update:title', this.title2); this.$emit('update:summary', this.summary2); } } }); var app = new Vue({ el: "#app", data: { obj: { title: "TITLE", summary: "SUMMARY" } } }); </script> </body> </html>
在线代码编辑器
HTML / CSS / JavaScript 实时预览
更多工具
运行
保存
自动执行
模板
JS 库
清空
主题
代码编辑区
字符数: 0
提示: 输入完整的 HTML 代码,需包含 DOCTYPE 声明与 html 根标签
预览效果
选择模板
取消
选择库和版本
库
版本
文件
取消
已复制到剪贴板